how to find the location of element in matrix?
Show older comments
my code:
I=[2 3 10 4 6; 1 4 7 5 3; 5 2 8 4 3;8 2 1 7 3;1 9 8 3 4;];
D = padarray(I,[1 1],0,'both');
[x y]=size(D);
m=1;
n=1;
for i=2:x-1
for j=2:y-1
A=[D(i-1,j-1:j+1),D(i,j-1),D(i,j+1),D(i+1,j-1:j+1)];
I1(m,n)=max(A(:));
%[maxval(m,n) index(m,n)]=max(A(:));
n=n+1;
end
m=m+1;
n=1;
end
[tc locatn]=ismember(I1,I);
s=[5,5];
[R C]=ind2sub(s,locatn');
Required output
locatn=[7 11 12 11 17; 3 11 11 11 21; 4 4 12 13 19; 10 10 10 13 19; 10 4 10 15 19]
i got the output as,
locatn=[7 11 12 11 3; 3 11 11 11 21; 4 4 12 4 12; 10 10 10 4 12; 10 4 10 4 12]
can anyone help me to get the required output? how to correct this code?
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 18 Oct 2013
Edited: Azzi Abdelmalek
on 18 Oct 2013
I think there is an error in your expected locatn, try this
I2=unique(I1(:));
ii=histc(I1(:),I2);
idx1=zeros(size(I));
for k=1:numel(I2)
a=I2(k)
jdx=ismember(I1,a);
j1=find(jdx,ii(k))';
idx=ismember(I,a);
i1=find(idx,ii(k));
i1(end:numel(j1))=i1(end);
idx1(j1)=i1;
end
locatn=idx1
2 Comments
sheno39
on 28 Oct 2013
Azzi Abdelmalek
on 28 Oct 2013
If you look for the location of 5 from I1 in the matrix I, there are two 5 in I1, we should find the locations of the two first 5 in I, that's what i1 represent.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!