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

답변 (2개)

Andrew Newell
Andrew Newell 2012년 1월 11일

0 개 추천

tf = ismember(A,B);
C = 0*A;
C(tf) = B(:,2);
M = [A C]
Andrei Bobrov
Andrei Bobrov 2012년 1월 11일

0 개 추천

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

Sara
Sara 2012년 1월 11일
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 2012년 1월 11일
see added
Sara
Sara 2012년 1월 12일
the code works well, its something with the data I have :(

이 질문은 마감되었습니다.

질문:

2012년 1월 11일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by