Convert subscripts to linear indices with dynamic matrix size
이전 댓글 표시
Hi all,
I have a matrix with a dynamic size, e.g. M x N. Each row of the matrix indicates subscripts of an element in a N-D matrix.
Do you know how to convert the subscript matrix into a linear index vector without listing the subscripts of every dimension as in sub2ind function?
Thanks!
채택된 답변
추가 답변 (1개)
Gaurav Garg
2020년 8월 10일
편집: Gaurav Garg
2020년 8월 10일
0 개 추천
Hi Soan,
You can loop over all the rows, which indicate subscripts and run sub2ind function; and store the results in an array.
댓글 수: 2
Soan Duong
2020년 8월 10일
Gaurav Garg
2020년 8월 10일
Soan,
Instead of using the ind2sub function, you can convert N-D array index to 1-D index. I would provide you with an example for N=2 -
For a 2-D array of dimensions M x N and an element A(i,j) in it-
A(i,j) = (j-1) * N + i
Since MATLAB stores data in column-major format, you would skip (j-1) columns resulting to (j-1)*N elements and ith element from here would be your resultant array.
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!