I have 2 matrices A and B, both 7x3. I want to compare each element in A with its corresponding element in B, taking the larger of the 2 values and creating a new Matrix C. Could someone suggest a method on how I would do this? I have seen examples of comparing to find equality between 2 elements but not a comparison to create a new matrix so I am finding this rather difficult.
Thanks in advanced.

 채택된 답변

Mischa Kim
Mischa Kim 2014년 1월 24일

0 개 추천

Use the max function. Try:
A = rand(3);
B = rand(3);
C = max(A,B)

추가 답변 (1개)

Kiran
Kiran 2014년 1월 24일

0 개 추천

Thanks for the answer, but this doesn't seem to be doing what I need, maybe I should be more clear.
For instance if i have A=[1 3 4;8 3 2;3 8 2]; B=[7 2 1;5 4 2; 6 1 8];
I need to compare each element of A with its corresponding element in B, so in elements (1,1) in A and B are 1 and 7 respectively. Of the 2 values, 7 is the largest and there will be the value being placed into the same position in matrix C. I must do the same for each element as above.
Hope this helps.

댓글 수: 3

Amit
Amit 2014년 1월 24일
Thats is what Mischa's answer will do. Give it a try.
Kiran
Kiran 2014년 1월 24일
I see this is sorting the matrix as I would like it to, however the numbers are not the ones in the matrix, entries are now non integers. Is there a reason for this?
Kiran
Kiran 2014년 1월 24일
Sorry, how silly of me, I realised rand was the operation for random and shouldn't be included. Code works fine, thanks very much for your help guys

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

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2014년 1월 24일

댓글:

2014년 1월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by