Set conditions between two matrices
Ältere Kommentare anzeigen
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 Kommentar
Jan
am 5 Jan. 2018
"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.
Akzeptierte Antwort
Weitere Antworten (1)
Pawel Jastrzebski
am 5 Jan. 2018
Bearbeitet: Pawel Jastrzebski
am 5 Jan. 2018
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';
Kategorien
Mehr zu Just for fun finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!