# location of element in matrix

4 views (last 30 days)
jano satti on 25 Apr 2017
Edited: Andrei Bobrov on 25 Apr 2017
I have a matrix mat and I want to find location of x in mat matrix.location should be in order of x.
mat =[
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.0099 1.0000 1.0000 0.0120 1.0000 0.0115
0.0093 1.0000 1.0000 0.0120 1.0000 0.0070
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.0098 1.0000 1.0000 0.0089 1.0000 0.0093
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000];
x =[
0.0089
0.0099
0.0070];
A =mat
C=x
numbersInA = unique(C)
numbersInC = unique(C)
for k = 1 : length(numbersInA)
[rowA colA] = find(A==numbersInC(k))
w(k,:)= [rowA , colA]
end
ans is [ 3 6]
[5 4]
[2 1]
but I need this solution::
[5 4]
[2 1]
[3 6]
##### 1 CommentShowHide None
Stephen23 on 25 Apr 2017

Andrei Bobrov on 25 Apr 2017
Edited: Andrei Bobrov on 25 Apr 2017
[l0,ii] = ismembertol(mat(:),x);
[i0,j0] = ind2sub(size(mat),find(l0));
[~,i1] = sort(ii(l0));
out = [i0,j0];
out = out(i1,:);

### More Answers (2)

Stephen23 on 25 Apr 2017
##### 1 CommentShowHide None
Andrei Bobrov on 25 Apr 2017
+1

Azzi Abdelmalek on 25 Apr 2017
Use ismember function