Help comparing two matrices

1 回表示 (過去 30 日間)
Antonio
Antonio 2014 年 12 月 23 日
コメント済み: Antonio 2014 年 12 月 23 日
so I need to compare two matrices but I need to set the values the final compared matrice will have... so for example if one value is higher than the other it should display 1; if it is lower -1 and finally if it is equal 0. I have been messing with this but cant figure it out I think I need a way to go over the two matrices comparing the two elements and but then how do I set those elements back into a matrice....? why is it so complicated!!!
well i need a function... sorry and thanks for the responses
  1 件のコメント
Image Analyst
Image Analyst 2014 年 12 月 23 日
You already asked this and got answers from Roger and Azzi, and you accepted one: here is your duplicate question, so why are you asking again??? For what it's worth, I like Roger's and Matt's solution of using sign() best.

サインインしてコメントする。

採用された回答

Amy
Amy 2014 年 12 月 23 日
You can use logical indexing for that. If m1 is the first matrix and m2 is the second and C is the comparison, I think you want something like:
C = zeros(size(m1));
C(m1>m2) = 1; % fill in 1 wherever m1 is greater than m2
C(m2>m1) = -1; % fill in -1 wherever m2 is greater
This assumes there are no nans. If that's the case you might initialize C to NaNs and then add a line for the case of m1==m2.
  1 件のコメント
Antonio
Antonio 2014 年 12 月 23 日
but how do you integrate that in a function?
function [C]=comparaMatrices(m1,m2)
.....
.....
I cant get it working...sorry im a noob

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2014 年 12 月 23 日

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by