MATLAB: Compare matrix and vector: for each cell in the matrix, get the index in the vector corresponding to the value in the cell.

compare matrix vector index

Hi!
I have to do the following task and I can't find an "efficient" way of doing it (i.e. I don't want to use a for-loop).
I have a matrix (A) and a vector (B). The vector B has unique values. Every value in matrix A is also in vector B.
I want to obtain a matrix C (with the same dimension of A) which in each cell has the index in vector B that corresponds to the value in that cell of matrix A.
For example:
A = [3 4 6 ;
4 4 3.5;
3 6 6]
B = [3 3.5 4 5 6]
C = [1 3 5;
3 3 2;
1 5 5]
Could you understand the task? Does anybody know how to do it?
Thank you very much in advance!
Santiago.

Best Answer

[~,C] = ismember(A,B)