Help comparing two matrices

5 ビュー (過去 30 日間)
Antonio
Antonio 2014 年 12 月 26 日
コメント済み: Azzi Abdelmalek 2014 年 12 月 26 日
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 was told the code is the following:
if
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
but i dont know how to get this into a working function so id you could help me...thanks
  1 件のコメント
Chad Greene
Chad Greene 2014 年 12 月 26 日
Remove the if line and it should work perfectly as you've written it, as long as m1 and m2 are the same size.

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

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 26 日
編集済み: Azzi Abdelmalek 2014 年 12 月 26 日
sign(m1-m2)
% Example
m1=[1 4;10 20]
m2=[0 2;10 40]
out=sign(m1-m2)
  2 件のコメント
Antonio
Antonio 2014 年 12 月 26 日
but what im looking for is working with m-file with the shape of:
function [...]=comparaMatrices(m1, m2)
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 26 日
function out=comparaMatrices(m1, m2)
out=sign(m1-m2)

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by