MATLAB: Finding row number of an array elements in related columns of a matrix

findmatrix

hi, I have a matrix A=[5 4 1 4;1 2 4 5; 4 5 5 1;1 1 2 2] and an array b=[1 4 5 2]. I want to find the row number of first occurrence of the array elements in related columns of the matrix as follows: output=[2,1,3,4]. "considering that my data is big and I want to do this without using any loop" Can anyone help? thank you

Best Answer

This is simple with the second output of max:
>> A = [5,4,1,4;1,2,4,5;4,5,5,1;1,1,2,2]
A =
5 4 1 4
1 2 4 5
4 5 5 1
1 1 2 2
>> b = [1,4,5,2]
b =
1 4 5 2
>> [~,idr] = max(bsxfun(@eq,A,b),[],1)
idr =
2 1 3 4
For MATLAB versions R2016b and later (with implicit scalar expansion):
[~,idr] = max(A==b,[],1)