Set conditions between two matrices
이전 댓글 표시
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
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.
채택된 답변
추가 답변 (1개)
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';
카테고리
도움말 센터 및 File Exchange에서 Just for fun에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!