how to find index of nx2 matrix by comparing elements

1 次查看(过去 30 天)
Hi, giving a matrix of size nx2
A=[ 222,186;
222,187;
223,187]
giving 2 values i= 222 and j=187 i want to find the index the row that contain i on the first column and j on the seond wich is 2 here i want something like
i thought to do somthing like this
for k=1:size(A,1)
test=find(A(k,1)==i && A(k,2)==j)
if (test==1)
ind=k;
break;
end
end
i need only the first index like i did here but i want to avoid the loop.
Thanks

采纳的回答

Jos (10584)
Jos (10584) 2016-5-31
A=[ 222,186;
222,187;
223,187]
i = 222
j = 187
% two alternatives
r1 = find(A(:,1)==i & A(:,2)==j))
[tf,r2] = ismember(A,[i j],'rows')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by