MATLAB: If the value is equal to a element in the matrix.

matrixmatrix array

Hi,
I have two matrices listed below.
A = [ 1 29000 ;
2 30000 ;
3 4200 ];
B = [ 1 1 2 2 2 ;
2 1 3 2 2 ;
3 1 4 2 2 ];
I want to wrtie a code that does the following: If column 5 of B equals to any of the number in column 1 of A, then output equals to the corresponding number in column 2 of A. Can anyone help with the coding?
Thanks in advance

Best Answer

A = [ 1 29000 ;
2 30000 ;
3 4200 ];
B = [ 1 1 2 2 2 ;
2 1 3 2 2 ;
3 1 4 2 2 ];
for i = 1:size(B,1)
idx = B(i,5)==A(:,1) ;
if any(idx)
A(idx,2)
end
end
YOu can use ismemebr...read about it.
[c,ia] = ismember(B(:,5),A(:,1))
iwant = A(ia,2)