compare the absolute value of each element from two matrices

조회 수: 10 (최근 30일)
Jack_111
Jack_111 2014년 11월 25일
댓글: Image Analyst 2014년 11월 25일
I have two matrices A and B with the size NxM and I want to compare the absolute value of each element for the two matrices and return the bigger value and then to store the real value in a new matrix.
Any suggestions please
Many thanks in advance
  댓글 수: 3
Jack_111
Jack_111 2014년 11월 25일
for example a = [1 -2 -3; 4 5 -6] and b = [3 2 1; 8 9 -3] and when I will compare the absolute values and store them in another matrix using c = max(abs(a),abs(b)); that means the output will be
c = [3 2 3 ; 8 9 6] but I want it to return c = [3 2 (doesn't matter) -3 ; 8 9 -6] (in the case where they are equal, it doesn't matter what it will return).
I hope you got my point. I don't want the absolute value to be registered but just the real value which is in our example (-6 and -3) nut the absolute value is just to compare
Image Analyst
Image Analyst 2014년 11월 25일
OK. Good. Calling it the "real" value instead of the "max" value was a major oversight initially. But now that you've clarified it, it looks like you now have two correct solutions. Thanks for accepting one of them. You can also vote for both of them too, even though you can accept only one.

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

채택된 답변

Guillaume
Guillaume 2014년 11월 25일
편집: Guillaume 2014년 11월 25일
Concatenate A and B along the third dimension, get the second output of max along the 3rd dimension of the absolute of that 3d matrix to get the z index. The tricky bit is then using that z value to select A or B. Summing each matrix multiplied by a logical matrix should do it:
a = [1 -2 -3; 4 5 -6]
b = [3 2 1; 8 9 -3]
ab = cat(3, a, b);
[~, idx] = max(abs(ab), [], 3);
c = a.*(idx==1) + b.*(idx==2)

추가 답변 (1개)

the cyclist
the cyclist 2014년 11월 25일
편집: the cyclist 2014년 11월 25일
absA = abs(A);
absB = abs(B);
I'm not sure what you mean by "compare ... and return the real value". Larger? Smaller? Difference?
  댓글 수: 2
Jack_111
Jack_111 2014년 11월 25일
편집: Jack_111 2014년 11월 25일
for example a = [1 -2 -3; 4 5 -6] and b = [3 2 1; 8 9 -3] and when I will compare the absolute values and store them in another matrix using c = max(abs(a),abs(b)); that means the output will be
c = [3 2 3 ; 8 9 6] but I want it to return c = [3 2 (doesn't matter) -3 ; 8 9 -6] (in the case where they are equal, it doesn't matter what it will return).
I hope you got my point. I don't want the absolute value to be registered but just the real value which is in our example (-6 and -3) nut the absolute value is just to compare
Jack_111
Jack_111 2014년 11월 25일
Sorry that my question wasn't phrased well

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by