how to find the location of element in matrix?
이전 댓글 표시
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?
채택된 답변
추가 답변 (1개)
Azzi Abdelmalek
2013년 10월 18일
편집: Azzi Abdelmalek
2013년 10월 18일
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
sheno39
2013년 10월 28일
Azzi Abdelmalek
2013년 10월 28일
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.
카테고리
도움말 센터 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!