if else statement for elemental comparison

조회 수: 2 (최근 30일)
Emily Pendleton
Emily Pendleton 2018년 1월 24일
댓글: Emily Pendleton 2018년 1월 25일
Hello,
I have 2 matrices, a & b, both 1024x1024. I am trying to create an "if/else" statement where each element in the same position in the two matrices are compared to each other. Depending on which value is greater, the subsequent analysis is performed. I have the following code, but it is currently running with "any" value in the matrix. Is there a way to do if/else on an elemental basis? I then want to find the average of all the resulting values (here, average of "ab" and "ba.") Is there a better way to do this than my current methods? This is what I have so far (and thanks for your help!):
a = load('0.txt');
b = load('9.txt');
if any(a > b)
ab = (a-b)./(a+(2*b));
abavg= mean(mean(ab,'omitnan'), 'omitnan')
abSD= std2(ab);
else
ba = (b-a)./(b+(2*a));
baavg= mean(mean(ba,'omitnan'), 'omitnan')
baSD= std2(ba);
end
  댓글 수: 4
Benjamin Kraus
Benjamin Kraus 2018년 1월 24일
You need to convert your matrix into a vector first. One way to do that is using :.
mean(ba(:))
Emily Pendleton
Emily Pendleton 2018년 1월 24일
Got the mean part, thanks.
Still, I think I'm mis-using the logical indexing (I'm only getting values for ab and none for ba. I could check by hand if I should have values for ba, but it would take a while).
Did I appropriately use the logical indexing here? When I copied/pasted the answer from Rik Wisselink after "if", I got an error message.
if a(a>b)
ab = (a-b)./(a+(2*b));
abavg = mean(ab(:), 'omitnan')
abSD= std2(ab);
else
ba = (b-a)./(b+(2*a));
baavg = mean(ba(:), 'omitnan')
baSD= std2(ba);
end

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2018년 1월 24일
The logical indexing shouldn't be used in combination with an if-statement. Let me give you a small example:
a=[3 5 6];
b=[5 4 8];
%then a>b results in [false true false] (or [0 1 0])
%a([0 1 0]) is 5, so a(a>b) is 5
%a(a<b) is [3 6]
That is called logical indexing, because you use a logical array to index another array.
The code below should solve your problem.
%dummy data
a=rand(1024);
b=rand(1024);
%create shorthand arrays with logical indexing
a1=a(a>b);
a2=a(a<=b);
b1=a(a>b);
b2=a(a<=b);
ab = (a1-b1)./(a1+(2*b1));
abavg = mean(ab(:), 'omitnan');
abSD= std2(ab);
ba = (b2-a2)./(b2+(2*a2));
baavg = mean(ba(:), 'omitnan');
baSD= std2(ba);

추가 답변 (1개)

Benjamin Kraus
Benjamin Kraus 2018년 1월 24일
You need to calculate both answers, then recombine the matrix using logical indexing. Something like this:
ab = (a-b)./(a+(2*b));
abavg = mean(ab(:), 'omitnan')
abSD= std2(ab);
ba = (b-a)./(b+(2*a));
baavg = mean(ba(:), 'omitnan')
baSD= std2(ba);
out1 = ab;
out1(b>a) = ba(b>a); % logical indexing to replace only values where |b>a|
avg = abavg;
avg(b>a) = baavg(b>a); % logical indexing to replace only values where |b>a|
SD = abSD;
SD(b>a) = abavg(b>a); % logical indexing to replace only values where |b>a|
  댓글 수: 2
Benjamin Kraus
Benjamin Kraus 2018년 1월 24일
Actually, looking back at your code a little closer, this may be what you are trying to do:
ab = (a-b)./(a+(2*b));
abavg = mean(ab(a>b), 'omitnan')
abSD= std2(ab(a>b));
ba = (b-a)./(b+(2*a));
baavg = mean(ba(b>a), 'omitnan')
baSD= std2(ba(b>a));
Also, don't forget about places where a == b.
Emily Pendleton
Emily Pendleton 2018년 1월 25일
thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by