If then Statement For Matrix Math

Hello, I currently have two txt files loaded as matrices that are 1000x1024 pixels. The code I have below includes math that I want to do at each pixel location for the matrices. However, I want to either subtract the second matrix pixel from the first (if the second matrix pixel is larger) or subtract the first matrix pixel from the second (if the first matrix pixel is larger). Therefore, I am assuming that my code is going in the wrong direction and that I need to do a if/then statement.
a = load('File1.txt'); b = load('File2.txt');
a1=a(a>b); a2=a(a<=b); b1=b(a>b); b2=b(a<=b);
ab = (a1-b1)./(a1+(2*b1)); ba = (b2-a2)./(b2+(2*a2)); ABBA = [ab; ba];
As you can see in this code, I am trying to either do calculation ab if the pixel at hand is larger in the first array or ba if the pixel is larger in the second array. Then, my end goal is to have one array as the output (1000x1024) with the answer to either ab calculation or ba calculation. Because any pixel can be larger in either the first or second array, I am not sure how to go about this. Any help is greatly appreciated, thank you!

4 commentaires

RB
RB le 6 Mar 2018
Because the calculations are different: if a is larger then we want to do a minus b divided by a+(2 times b). If b is larger it is the other way around, so absolute value won't give us what we desire. Therefore, I still believe I have to construct an if then statement.
Because you want to look at individual pixels my first thought would be to run a loop, then include an if statement.
for I = 1:size(a,1); % rows
for j = 1:size(a,2); % columns
if a(I,j)>b(I,j)
ABBA(I,j) = (a(I,j)-b(I,j))/(a(I,j)+2*b(I,j));
else
ABBA(I,j) = (b(I,j)-a(I,j))/(b(I,j)+2*a(I,j));
end
end
end
RB
RB le 6 Mar 2018
Yes that seems to do the trick! Thank you for your help!
RB
RB le 9 Mar 2018
I am somehow still getting negative values for this and I am not sure what is going wrong with the code: according to the code it should only give positive values because it subtracts only the value that is less than the other value. Do you know what could be going wrong here?

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 6 Mar 2018
A = 8*ones(4)
B = magic(4)
X = max(A, B)
N = min(A, B)
Work with X and N.

Catégories

Question posée :

RB
le 6 Mar 2018

Commenté :

RB
le 9 Mar 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by