Set conditions between two matrices

3 ビュー (過去 30 日間)
Camille Fong
Camille Fong 2018 年 1 月 4 日
編集済み: Guillaume 2018 年 1 月 5 日
First question :* I have two matrices (360x1) and I want to set conditions to generate a third matrix (360x1). I want to compare the first row of the first matrice [A] to the first row of the second matrice [B], up until the 360th row of the first matrix to the 360th rows of the second matrix.
This is what I tried to write:
E = ones(360,1);
for r=1:360
for c=1:1
for k=1:360
if A(k,1) == B(k,1)
E(r,c)= 2;
elseif A(k,1) > B(k,1)
E(r,c)= -1;
else A(k,1) < B(k,1)
E(r,c)= 0;
end
end
end
...and it doesn't work.
Second question : I would like to display a different text instead of the value '2', '-1' and '0'.
Thank you in advance !
  1 件のコメント
Jan
Jan 2018 年 1 月 5 日
"It doesn't work" is too lean to describe the problem you have. Do you get an error message or does the result differ from your expectations? Currently the comparison does not depend on r.

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

採用された回答

Guillaume
Guillaume 2018 年 1 月 5 日
If you're happy with having 1 when A(k)>B(k), 0 when A(k)==B(k) and -1 when A(k)<B(k), then the answer is simply:
E = sign(A-B)
No loop needed. If you do want -1, 2, 0 in the above case, respectively, then:
vals = [0 2 -1];
E = vals(sign(A-B) + 2)
"I would like to display a different text instead of the value '2', '-1' and '0'"
First, you're mixing up text and numeric. Your code, and the answers above, generate numerical arrays. There is no text. You could have text instead, e.g.:
vals = {'smaller', 'equal', 'greater'}; %for text, has to be a cell array
E = vals(sign(A-B) + 2)
but be aware that working with text is a bit harder than working with numerical matrices.
Finally, if you're working with vectors use linear indexing (i.e. A(k)) rather than subscript indexing (i.e A(k, 1)). The former will work regardless of the direction of the vector, the latter will not.
  3 件のコメント
Jan
Jan 2018 年 1 月 5 日
+1. Maybe Camille Fong want to compare the vectors elementwise like:
E = sign(A - B.') % >= Matlab R2016b, auto-expanding
to get E as a matrix. For older Matlab versions:
E = sign(bsxfun(@minus, A, B.'))
Guillaume
Guillaume 2018 年 1 月 5 日
編集済み: Guillaume 2018 年 1 月 5 日
Yes, I wondered about that but Camille did state she wanted a 360x1 result. The r loop is indeed puzzling in the original code.

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

その他の回答 (1 件)

Pawel Jastrzebski
Pawel Jastrzebski 2018 年 1 月 5 日
編集済み: Pawel Jastrzebski 2018 年 1 月 5 日
clear all;
clc;
% DATA
A = randi(50,1,360,'int16');
B = randi(50,1,360,'int16');
% LOGICAL VECTORS
AB_Equal = A == B
A_Greater = A > B
B_Greater = ~(AB_Equal | A_Greater)
% NEW MATRIX
C = zeros(1,length(A));
C(A == B) = 2;
C(A > B) = -1;
% IF you want to swap the -1,0,2 for text classification, i.e.:
% -1 = 'a'
% 0 = 'b'
% 2 = 'c'
cVal = '0';
cTxt = repmat(cVal,1,length(A));
cTxt(AB_Equal) = 'c';
cTxt(A_Greater) = 'a';
cTxt(B_Greater) = 'b';

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by