Set conditions between two matrices

3 visualizaciones (últimos 30 días)
Camille Fong
Camille Fong el 4 de En. de 2018
Editada: Guillaume el 5 de En. de 2018
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 comentario
Jan
Jan el 5 de En. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 5 de En. de 2018
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 comentarios
Jan
Jan el 5 de En. de 2018
+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 el 5 de En. de 2018
Editada: Guillaume el 5 de En. de 2018
Yes, I wondered about that but Camille did state she wanted a 360x1 result. The r loop is indeed puzzling in the original code.

Iniciar sesión para comentar.

Más respuestas (1)

Pawel Jastrzebski
Pawel Jastrzebski el 5 de En. de 2018
Editada: Pawel Jastrzebski el 5 de En. de 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';

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by