sub2ind() errors out with NaN inputs - handled differently since R2024a
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Eric Machorro
am 4 Mär. 2025
Kommentiert: Eric Machorro
am 7 Mär. 2025
>> 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 Kommentare
Walter Roberson
am 4 Mär. 2025
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.
Akzeptierte Antwort
Matt J
am 4 Mär. 2025
You can get the old behavior back if you need it,
Sub2Ind([ 3 5 7], [ 3 3], [2 2], [1 nan])
function out=Sub2Ind(sz,varargin)
out=varargin{1};
for i=2:numel(varargin)
out=out+(varargin{i}-1)*(sz(i-1));
end
end
Weitere Antworten (1)
Steven Lord
am 4 Mär. 2025
This was a bug fix introduced in release R2024a.
3 Kommentare
Steven Lord
am 7 Mär. 2025
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)
Perhaps the error message could be more descriptive.
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!