Set conditions between two matrices

조회 수: 8 (최근 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 CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by