sub2ind() errors out with NaN inputs - handled differently since R2024a

조회 수: 12 (최근 30일)
>> matlabRelease
ans =
matlabRelease with properties:
Release: "R2022b"
Stage: "release"
Update: 3
Date: 17-Nov-2022
>> sub2ind([ 3 5 7], [ 3 3], [2 2], [1 nan])
ans =
6 NaN
versus
>> matlabRelease
ans =
matlabRelease with properties:
Release: "R2024b"
Stage: "release"
Update: 4
Date: 26-Dec-2024
>> sub2ind([ 3 5 7], [ 3 3], [2 2], [1 nan])
Error using sub2ind (line 71)
Out of range subscript.
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 3월 4일
That change probably came with R2024a, which had the following documented change to sub2ind:
The sub2ind (R2024a) function now supports scalar expansion and accepts a mix of scalars and vectors for subscript inputs. For example, sub2ind(sz,[1 2 3],2) is now the same as sub2ind(sz,[1 2 3],[2 2 2]). Previously, the subscript inputs were required to be the same size.
Eric Machorro
Eric Machorro 2025년 3월 4일
I suspect you are correct - but then the issue is that R2024a added some nice functionality, yet removed a minor aspect of its backward compatibility. If that removal wasn't intentional, maybe it could be undone? Either way I would think it should be documented or fixed in subsequent release.

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

채택된 답변

Matt J
Matt J 2025년 3월 4일
You can get the old behavior back if you need it,
Sub2Ind([ 3 5 7], [ 3 3], [2 2], [1 nan])
ans = 1×2
6 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function out=Sub2Ind(sz,varargin)
out=varargin{1};
for i=2:numel(varargin)
out=out+(varargin{i}-1)*(sz(i-1));
end
end
  댓글 수: 1
Eric Machorro
Eric Machorro 2025년 3월 7일
Thank you - looking at the original code for this , i would've expected something way more complicated. Thank you very much.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2025년 3월 4일
This was a bug fix introduced in release R2024a.
  댓글 수: 3
Steven Lord
Steven Lord 2025년 3월 7일
Generally, my mental model for the workflow for sub2ind (from the name) is that what comes out ought to be a valid linear index into an array that selects the same element as the subscripts you passed into the function. NaN is neither a valid subscript nor a valid linear index. Nor is 0.
I = sub2ind([10 10], 1, 0)
Error using sub2ind (line 71)
Out of range subscript.
Perhaps the error message could be more descriptive.
Eric Machorro
Eric Machorro 2025년 3월 7일
That is one way to handle it. And I agree that is a rather uninformative error message. But, it is a little deeper than that I think. If, by off chance, it it fed in a nan value, it could (and did prior to R2024a) return a nan for the appropriate index. The newer version removed that functionality, but added another facet of funcationality in being able to handle inputs of different sizes.
This would provide a higher degree of backward compatibility it would seem. But perhaps I am missing something. Not a major deal I suppose.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by