How does sub2ind work with non-integer values?

조회 수: 3 (최근 30일)
Saygin
Saygin 2025년 7월 21일
편집: David Goodmanson 2025년 7월 25일
I was working with some displacement matrices and noticed sub2ind working with non-integer inputs. It not only works but also outputs non-integer values. I looked at the documentation but didn't see an explanation for this behavior.
For example:
sub2ind([2 2],1,1.8)
ans = 2.6000
So, if I round the output to 3, is it going to be the linear index that is closest to (1,1.8) based on Euclidean distance? What is the intended behavior here?
Thanks in advance and apologies if I missed this in the documentation.
  댓글 수: 12
David Goodmanson
David Goodmanson 2025년 7월 25일
편집: David Goodmanson 2025년 7월 25일
Hi Matt, after you commented on noninversion I added some material on the topic (more or less) in my answer below.
Paul
Paul 2025년 7월 25일
"I wonder if the developers assumed that the output of sub2ind would be used only for indexing and were therefore relying on the non-integer case to throw an error downstream when used in an actual indexing operation."
If that were the case, I guess it would have been a mistake insofar as a valid output can be obtained from an invalid input
sub2ind([100,100],61,10)
ans = 961
sub2ind([100,100],11,10.5)
ans = 961

댓글을 달려면 로그인하십시오.

답변 (1개)

David Goodmanson
David Goodmanson 2025년 7월 21일
편집: David Goodmanson 2025년 7월 25일
Hi Saygin,
Interesting behavior. The help for sub2ind says it 'returns the linear index equivalent' and I guess they really mean it about the linear part. For 2d, If the input matrix size is mxn and the input indices are p,q then the result is
sub2ind([m,n],p,q) = p + (q-1)*m
so long as 1<=p<=m and 1<=q<=n, even if none of them (including m and n) are integers. Evidently Matlab only checks the inequalities just listed, not for being integers.
---------
Matt's comment 'ind2sub does not invert sub2ind when non-integer arguments are permitted' brought up the idea of what kind of process could be inverted.
Suppose the matrix is size mxn, and r and c are row and column indices. ind2sub allows any of those four quantities to be noninteger. Let j be the index,
j = sub2ind([m n],r,c)
and let k be the index you would get if you read the matrix out rowwise rather than columnwise. Or if you prefer, by reading out the transposed version in the usual manner,
k = sub2ind([n m],c,r)
Let 'bar' quantities be one less than actual quantites; rbar = r-1 etc. Turn r and c into a 2x1 vector, same with j and k. Then there is a nice relationship among the barred quantities
m = 3.4; n = 2.7; r = 3.1; c = 1.8;
j = sub2ind([m n],r,c)
s2i = [1 m;n 1];
rcbar = [r;c]-1;
jkbar = s2i*rcbar; % fwd
jk = jkbar+1 % jk(1) = j
rcbar = s2i\jkbar; % inv
rc = rcbar+1 % agrees
j = 5.8200
jk = 5.8200
7.4700
rc = 3.1000
1.8000

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by