location of the i'th element
조회 수: 18 (최근 30일)
이전 댓글 표시
victor hanna
2019년 12월 10일
댓글: JESUS DAVID ARIZA ROYETH
2019년 12월 10일
hi
A is a matrix
Iam using a for loop on all the elements in A like this
n = numel(A);
for i = 1 : n
A(i);
end
but i need the element location in the original matrix
so my quastion is there away to find the i'th elements location in A ?
or if there is another way to run loop on all the elements?
댓글 수: 2
Stephen23
2019년 12월 10일
"i need the element location in the original matrix"
And you have got it already: i is its linear index.
"is there away to find the i'th elements location in A"
The linear index of the i-th element of A is i.
Adam
2019년 12월 10일
What do you mean by 'location'? i is its location as a 1d index.
You can use
doc ind2sub
to convert to 2d subscripts if you want, or just do the double loop around both dimensions. Or preferably don't do a loop at all, but that would depend entirely what you are actually doing in the loop body.
채택된 답변
JESUS DAVID ARIZA ROYETH
2019년 12월 10일
편집: JESUS DAVID ARIZA ROYETH
2019년 12월 10일
you can use nested loops
for i = 1 : size(A,1)
for j=1:size(A,2)
A(i,j)
end
end
with a loop:
n = numel(A);
for i = 1 : n
[row,col]=ind2sub(size(A),i);
A(row,col)
end
댓글 수: 2
JESUS DAVID ARIZA ROYETH
2019년 12월 10일
n = numel(A);
for i = 1 : n
[row,col]=ind2sub(size(A),i);
A(row,col)
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!