unequal vectors

I have a vector A for example of size 1500x1
matrix B size 817x2
A and B are numeric
All elements in column 1 in B are subset of A and all elements of B and A are uniquely identified.
I want to generate a matrix M(1500x2) such that the second column of M contains the elements of column 2 in B and zero whenever column1 in B isnot a member of A.
A= 10 11 12 13 14 15 16 17 ...
B column 1 is like 10 13 16
B column 2 is like 999 900 950
M col 1 is like 10 11 12 13 14 15 16 17
M col 2 is like 999 0 0 900 0 0 950 0
thx

Respuestas (2)

Andrew Newell
Andrew Newell el 11 de En. de 2012

0 votos

tf = ismember(A,B);
C = 0*A;
C(tf) = B(:,2);
M = [A C]
Andrei Bobrov
Andrei Bobrov el 11 de En. de 2012

0 votos

variant
M = A*[1 0]
M(ismember(A,B(:,1)),2) = B(:,2)
ADD [11:47(UTC+4) 11.01.2012]
can so?
A = [10 11 12 13 14 15 16 17]
B = [10 13 16; 999 900 950]
M = [1;0]*A
M(2,ismember(A,B(1,:))) = B(2,:)
EDIT [11:46(UTC+4) 18.12.2012]
[loga,idx] = ismember(A,B(:,1));
M = A*[1 0];
M(loga,2) = B(idx(loga),2);

3 comentarios

Sara
Sara el 11 de En. de 2012
Hi,
If I go with the code above I get a dimension mismatch error, this is because the left side of the second line is of size 1500x2 whereas the right handside is 817x1.
Am I missing anything here?
Andrei Bobrov
Andrei Bobrov el 11 de En. de 2012
see added
Sara
Sara el 12 de En. de 2012
the code works well, its something with the data I have :(

La pregunta está cerrada.

Etiquetas

Preguntada:

el 11 de En. de 2012

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by