how to convert 1D element into 2D element in a matirx?

hi, i need to find the location of elements in a matrix as (row,column) value. what function is used to get the row and column of a element?

2 Comments

Your title does not match the content of your question
sir, i need to find the row,column value for each element in a matrix

Sign in to comment.

 Accepted Answer

Use find function
doc find
Example
A=[1 2 3;4 5 6;7 8 9]
[ii,jj]=find(A)

More Answers (1)

A = [4 0 3 0
4 4 4 -3
-3 -3 2 -3
5 0 -1 5]
s = size(A);
[irow,icol] = ndgrid(1:s(1),1:s(2));
out = [irow(:),icol(:),A(:)];

3 Comments

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 u help me to get the required output? how to correct this code?
Can you explain, for example, why, for locatn(1,5) , it should be 17 and not 3

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!