Error "Array indices must be positive integers or logical values" occurs at the line [row1, col1, s1] = ind2sub(size(chi_coeff), idx1);

3 次查看(过去 30 天)
I am working on a MATLAB script to find the minimum value in a 3D matrix chi_coeff and convert the linear index to subscripts using ind2sub. Despite idx1 being a valid scalar positive integer index, and chi_coeff confirmed as a 3D double array, I get the error:
Array indices must be positive integers or logical values.
on the line:
[row1, col1, s1] = ind2sub(size(chi_coeff), idx1);
Diagnostic checks I performed:
  • idx1 is scalar, positive, and integer (assertion passed).
  • chi_coeff is a valid 3D numeric array with size 5x10x4 and double class.
  • which ind2sub -all confirms the built-in function is used.
  • The error is not caused by passing a non-integer or invalid index to ind2sub.
The error still persists and might be coming from subsequent lines where I use (row1, col1, s1) to index other arrays of the same size, e.g.:
matlab
found_Scat_Calc(id,:) = Scat_Coeff_Calc(row1, col1, s1);
I suspect there might be a mismatch in array dimensions or an unexpected indexing issue.
Key code snippet:
[min_val1, idx1] = min(chi_coeff(:));
[row1, col1, s1] = ind2sub(size(chi_coeff), idx1);
found_Scat_Calc(id,:) = Scat_Coeff_Calc(row1, col1, s1);
Additional info:
  • Confirmed sizes with whos and class commands; all arrays involved have compatible dimensions.
  • The arrays involved in indexing are initialized with zeros and filled inside nested loops with parfor.
Would appreciate guidance on why this error occurs in the context of ind2sub and further indexing, and how to properly diagnose/fix dimension or indexing mismatch issues.
  1 个评论
Walter Roberson
Walter Roberson 2025-7-26
By the way, the recommended syntax these days for that operation would be
[min_val1, idx1] = min(chi_coeff, [], "all", "linear");
but personally it feels to me that surely that must require more processing than what you are using.

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2025-7-26
My guess is that you have a variable names 'size' and that is throwing the error.
Run this to test that hypothesis:
which size -all
built-in (/MATLAB/toolbox/matlab/elmat/size) built-in (/MATLAB/toolbox/matlab/elmat/@double/size) % double method built-in (/MATLAB/toolbox/matlab/elmat/@int16/size) % int16 method built-in (/MATLAB/toolbox/matlab/elmat/@int32/size) % int32 method built-in (/MATLAB/toolbox/matlab/elmat/@int64/size) % int64 method built-in (/MATLAB/toolbox/matlab/elmat/@int8/size) % int8 method built-in (/MATLAB/toolbox/matlab/elmat/@logical/size) % logical method built-in (/MATLAB/toolbox/matlab/elmat/@single/size) % single method built-in (/MATLAB/toolbox/matlab/elmat/@uint16/size) % uint16 method built-in (/MATLAB/toolbox/matlab/elmat/@uint32/size) % uint32 method built-in (/MATLAB/toolbox/matlab/elmat/@uint64/size) % uint64 method built-in (/MATLAB/toolbox/matlab/elmat/@uint8/size) % uint8 method size is a built-in method % string method /MATLAB/toolbox/matlab/lang/+matlab/+mixin/@Scalar/Scalar.m % dictionary method /MATLAB/toolbox/matlab/datatypes/categorical/@categorical/size.m % categorical method /MATLAB/toolbox/matlab/datatypes/tabular/@tabular/size.m % tabular method /MATLAB/toolbox/matlab/datatypes/datetime/@datetime/datetime.m % datetime method /MATLAB/toolbox/matlab/datatypes/duration/@duration/duration.m % duration method size is a built-in method % matlab.internal.capability.Capability method size is a built-in method % connector.internal.LoggerLevel method size is a built-in method % containers.Map method size is a built-in method % matlab.lang.OnOffSwitchState method /MATLAB/toolbox/matlab/indexing/+matlab/+mixin/+indexing/@RedefinesParen/RedefinesParen.m % matlab.mixin.indexing.RedefinesParen method size is a built-in method % simulink.FindSystemTask.Status method size is a built-in method % mf.zero.meta.Language method size is a built-in method % mf.zero.meta.PropertyIterabilityKind method size is a built-in method % dig.config.CommandType method size is a built-in method % dig.config.HorizontalAlignment method size is a built-in method % dig.config.ResourceState method size is a built-in method % dig.config.ResourceType method size is a built-in method % dig.model.DisplayState method size is a built-in method % dig.model.EventDataType method size is a built-in method % dig.model.FunctionType method size is a built-in method % dig.model.ValidInBdType method size is a built-in method % dig.model.ViewMode method size is a built-in method % codertarget.targethardware.BaseProductID method size is a built-in method % matlab.automation.Verbosity method size is a built-in method % matlab.unittest.internal.fixtures.FolderScope method size is a Java method % java.util.ArrayList method size is a Java method % java.util.AbstractList method size is a Java method % java.util.AbstractCollection method size is a Java method % java.util.Collection method size is a Java method % java.util.List method size is a Java method % java.util.HashMap method size is a Java method % java.util.AbstractMap method size is a Java method % java.util.Map method size is a built-in method % matlab.internal.timer.CallBackTypeEnum method size is a built-in method % matlab.internal.timer.BusyModeEnum method size is a built-in method % matlab.internal.timer.ExecutionModeEnum method size is a Java method % java.util.Collections$UnmodifiableMap method size is a Java method % java.util.Collections$UnmodifiableSet method size is a Java method % java.util.Collections$UnmodifiableCollection method size is a Java method % java.util.Set method size is a Java method % com.mathworks.hg.util.InfoPanel method size is a Java method % com.mathworks.mwswing.MJPanel method size is a Java method % javax.swing.JPanel method size is a Java method % javax.swing.JComponent method size is a Java method % java.awt.Container method size is a Java method % java.awt.Component method size is a built-in method % matlab.graphics.chart.datatype.ChartActivePositionType method size is a built-in method % matlab.internal.example.docparser.ExampleType method /MATLAB/toolbox/matlab/bigdata/@tall/size.m % tall method built-in % gpuArray method built-in (/MATLAB/toolbox/matlab/strfun/@string/size) % string method /MATLAB/toolbox/matlab/polyfun/@triangulation/size.m % triangulation method /MATLAB/toolbox/matlab/polyfun/@TriRep/size.m % TriRep method /MATLAB/toolbox/matlab/serial/@serial/size.m % serial method /MATLAB/toolbox/matlab/timeseries/@tscollection/size.m % tscollection method /MATLAB/toolbox/coder/half/@half/size.p % half method /MATLAB/toolbox/ident/ident/@iddata/size.m % iddata method /MATLAB/toolbox/imaq/imaq/@videoinput/size.m % videoinput method /MATLAB/toolbox/instrument/instrument/@udp/size.m % udp method /MATLAB/toolbox/instrument/instrument/@icgroup/size.m % icgroup method /MATLAB/toolbox/instrument/instrument/@tcpip/size.m % tcpip method /MATLAB/toolbox/instrument/instrument/@Bluetooth/size.m % Bluetooth method /MATLAB/toolbox/instrument/instrument/@gpib/size.m % gpib method /MATLAB/toolbox/instrument/instrument/@LegacyIcdevice/size.m % LegacyIcdevice method /MATLAB/toolbox/instrument/instrument/@visa/size.m % visa method /MATLAB/toolbox/instrument/instrument/@i2c/size.m % i2c method /MATLAB/toolbox/mbc/mbcdata/@cgoppoint/size.m % cgoppoint method /MATLAB/toolbox/mbc/mbcdesign/@candidateset/size.m % candidateset method /MATLAB/toolbox/mbc/mbcdesign/@coninputfactor/size.m % coninputfactor method /MATLAB/toolbox/mbc/mbcdesign/@designdev/size.m % designdev method /MATLAB/toolbox/mbc/mbcdesign/@xregdesign/size.m % xregdesign method /MATLAB/toolbox/mbc/mbcguitools/@xreglistctrl/size.m % xreglistctrl method /MATLAB/toolbox/mbc/mbcguitools/@xregtable/size.m % xregtable method /MATLAB/toolbox/mbc/mbcmodels/@localmulti/size.m % localmulti method /MATLAB/toolbox/mbc/mbcmodels/@localbspline/size.m % localbspline method /MATLAB/toolbox/mbc/mbcmodels/@xregunispline/size.m % xregunispline method /MATLAB/toolbox/mbc/mbcmodels/@xregmultilin/size.m % xregmultilin method /MATLAB/toolbox/mbc/mbcmodels/@localtruncps/size.m % localtruncps method /MATLAB/toolbox/mbc/mbcmodels/@localusermod/size.m % localusermod method /MATLAB/toolbox/mbc/mbcmodels/@xregmodel/size.m % xregmodel method /MATLAB/toolbox/mbc/mbcmodels/@localsurface/size.m % localsurface method /MATLAB/toolbox/mbc/mbcmodels/@xregtwostage/size.m % xregtwostage method /MATLAB/toolbox/mbc/mbcmodels/@xregusermod/size.m % xregusermod method /MATLAB/toolbox/mbc/mbcmodels/@xreglinear/size.m % xreglinear method /MATLAB/toolbox/mbc/mbcmodels/@xregmulti/size.m % xregmulti method /MATLAB/toolbox/mbc/mbcmodels/@localpspline/size.m % localpspline method /MATLAB/toolbox/mbc/mbctools/@sweepset/size.m % sweepset method /MATLAB/toolbox/mbc/mbctools/@xregmonitorplotproperties/size.m % xregmonitorplotproperties method /MATLAB/toolbox/mbc/mbctools/@sweepsetfilter/size.m % sweepsetfilter method /MATLAB/toolbox/mbc/mbctools/@xregdataset/size.m % xregdataset method /MATLAB/toolbox/mpc/mpc/@explicitMPC/size.m % explicitMPC method /MATLAB/toolbox/mpc/mpc/@mpc/size.m % mpc method /MATLAB/toolbox/nnet/deep/deep/@dlarray/size.m % dlarray method /MATLAB/toolbox/parallel/array/distributed/@codistributed/size.m % codistributed method /MATLAB/toolbox/parallel/array/distributed/@distributed/size.m % distributed method /MATLAB/toolbox/parallel/gpu/gpu/@gpuArray/size.m % gpuArray method /MATLAB/toolbox/parallel/lang/@Composite/size.m % Composite method /MATLAB/toolbox/robust/rctobsolete/robust/@icsignal/size.m % icsignal method /MATLAB/toolbox/shared/controllib/engine/@InputOutputModel/size.m % InputOutputModel method /MATLAB/toolbox/shared/statslib/@dataset/size.m % dataset method
You should get something like this result.
If the first entry is 'size is a variable' you have found the problem. The solution is to rename your variable to something meaningful in the context of your code that does not conflict with any MATLAB function names.
To illustrate --
size = 42;
idx1 = 84;
chi_coeff = rand(5,10,4);
which size -all
size is a variable. built-in (/MATLAB/toolbox/matlab/elmat/@double/size) % Shadowed double method built-in (/MATLAB/toolbox/matlab/elmat/@int16/size) % Shadowed int16 method built-in (/MATLAB/toolbox/matlab/elmat/@int32/size) % Shadowed int32 method built-in (/MATLAB/toolbox/matlab/elmat/@int64/size) % Shadowed int64 method built-in (/MATLAB/toolbox/matlab/elmat/@int8/size) % Shadowed int8 method built-in (/MATLAB/toolbox/matlab/elmat/@logical/size) % Shadowed logical method built-in (/MATLAB/toolbox/matlab/elmat/@single/size) % Shadowed single method built-in (/MATLAB/toolbox/matlab/elmat/@uint16/size) % Shadowed uint16 method built-in (/MATLAB/toolbox/matlab/elmat/@uint32/size) % Shadowed uint32 method built-in (/MATLAB/toolbox/matlab/elmat/@uint64/size) % Shadowed uint64 method built-in (/MATLAB/toolbox/matlab/elmat/@uint8/size) % Shadowed uint8 method size is a built-in method % Shadowed string method /MATLAB/toolbox/matlab/lang/+matlab/+mixin/@Scalar/Scalar.m % Shadowed dictionary method /MATLAB/toolbox/matlab/datatypes/categorical/@categorical/size.m % Shadowed categorical method /MATLAB/toolbox/matlab/datatypes/tabular/@tabular/size.m % Shadowed tabular method /MATLAB/toolbox/matlab/datatypes/datetime/@datetime/datetime.m % Shadowed datetime method /MATLAB/toolbox/matlab/datatypes/duration/@duration/duration.m % Shadowed duration method size is a built-in method % Shadowed matlab.internal.capability.Capability method size is a built-in method % Shadowed connector.internal.LoggerLevel method size is a built-in method % Shadowed containers.Map method size is a built-in method % Shadowed matlab.lang.OnOffSwitchState method /MATLAB/toolbox/matlab/indexing/+matlab/+mixin/+indexing/@RedefinesParen/RedefinesParen.m % Shadowed matlab.mixin.indexing.RedefinesParen method size is a built-in method % Shadowed simulink.FindSystemTask.Status method size is a built-in method % Shadowed mf.zero.meta.Language method size is a built-in method % Shadowed mf.zero.meta.PropertyIterabilityKind method size is a built-in method % Shadowed dig.config.CommandType method size is a built-in method % Shadowed dig.config.HorizontalAlignment method size is a built-in method % Shadowed dig.config.ResourceState method size is a built-in method % Shadowed dig.config.ResourceType method size is a built-in method % Shadowed dig.model.DisplayState method size is a built-in method % Shadowed dig.model.EventDataType method size is a built-in method % Shadowed dig.model.FunctionType method size is a built-in method % Shadowed dig.model.ValidInBdType method size is a built-in method % Shadowed dig.model.ViewMode method size is a built-in method % Shadowed codertarget.targethardware.BaseProductID method size is a built-in method % Shadowed matlab.automation.Verbosity method size is a built-in method % Shadowed matlab.unittest.internal.fixtures.FolderScope method size is a Java method % Shadowed java.util.ArrayList method size is a Java method % Shadowed java.util.AbstractList method size is a Java method % Shadowed java.util.AbstractCollection method size is a Java method % Shadowed java.util.Collection method size is a Java method % Shadowed java.util.List method size is a Java method % Shadowed java.util.HashMap method size is a Java method % Shadowed java.util.AbstractMap method size is a Java method % Shadowed java.util.Map method size is a built-in method % Shadowed matlab.internal.timer.CallBackTypeEnum method size is a built-in method % Shadowed matlab.internal.timer.BusyModeEnum method size is a built-in method % Shadowed matlab.internal.timer.ExecutionModeEnum method size is a Java method % Shadowed java.util.Collections$UnmodifiableMap method size is a Java method % Shadowed java.util.Collections$UnmodifiableSet method size is a Java method % Shadowed java.util.Collections$UnmodifiableCollection method size is a Java method % Shadowed java.util.Set method size is a Java method % Shadowed com.mathworks.hg.util.InfoPanel method size is a Java method % Shadowed com.mathworks.mwswing.MJPanel method size is a Java method % Shadowed javax.swing.JPanel method size is a Java method % Shadowed javax.swing.JComponent method size is a Java method % Shadowed java.awt.Container method size is a Java method % Shadowed java.awt.Component method size is a built-in method % Shadowed matlab.graphics.chart.datatype.ChartActivePositionType method size is a built-in method % Shadowed matlab.internal.example.docparser.ExampleType method /MATLAB/toolbox/matlab/bigdata/@tall/size.m % Shadowed tall method built-in % Shadowed gpuArray method built-in (/MATLAB/toolbox/matlab/strfun/@string/size) % Shadowed string method /MATLAB/toolbox/matlab/polyfun/@triangulation/size.m % Shadowed triangulation method /MATLAB/toolbox/matlab/polyfun/@TriRep/size.m % Shadowed TriRep method /MATLAB/toolbox/matlab/serial/@serial/size.m % Shadowed serial method /MATLAB/toolbox/matlab/timeseries/@tscollection/size.m % Shadowed tscollection method /MATLAB/toolbox/coder/half/@half/size.p % Shadowed half method /MATLAB/toolbox/ident/ident/@iddata/size.m % Shadowed iddata method /MATLAB/toolbox/imaq/imaq/@videoinput/size.m % Shadowed videoinput method /MATLAB/toolbox/instrument/instrument/@udp/size.m % Shadowed udp method /MATLAB/toolbox/instrument/instrument/@icgroup/size.m % Shadowed icgroup method /MATLAB/toolbox/instrument/instrument/@tcpip/size.m % Shadowed tcpip method /MATLAB/toolbox/instrument/instrument/@Bluetooth/size.m % Shadowed Bluetooth method /MATLAB/toolbox/instrument/instrument/@gpib/size.m % Shadowed gpib method /MATLAB/toolbox/instrument/instrument/@LegacyIcdevice/size.m % Shadowed LegacyIcdevice method /MATLAB/toolbox/instrument/instrument/@visa/size.m % Shadowed visa method /MATLAB/toolbox/instrument/instrument/@i2c/size.m % Shadowed i2c method /MATLAB/toolbox/mbc/mbcdata/@cgoppoint/size.m % Shadowed cgoppoint method /MATLAB/toolbox/mbc/mbcdesign/@candidateset/size.m % Shadowed candidateset method /MATLAB/toolbox/mbc/mbcdesign/@coninputfactor/size.m % Shadowed coninputfactor method /MATLAB/toolbox/mbc/mbcdesign/@designdev/size.m % Shadowed designdev method /MATLAB/toolbox/mbc/mbcdesign/@xregdesign/size.m % Shadowed xregdesign method /MATLAB/toolbox/mbc/mbcguitools/@xreglistctrl/size.m % Shadowed xreglistctrl method /MATLAB/toolbox/mbc/mbcguitools/@xregtable/size.m % Shadowed xregtable method /MATLAB/toolbox/mbc/mbcmodels/@localmulti/size.m % Shadowed localmulti method /MATLAB/toolbox/mbc/mbcmodels/@localbspline/size.m % Shadowed localbspline method /MATLAB/toolbox/mbc/mbcmodels/@xregunispline/size.m % Shadowed xregunispline method /MATLAB/toolbox/mbc/mbcmodels/@xregmultilin/size.m % Shadowed xregmultilin method /MATLAB/toolbox/mbc/mbcmodels/@localtruncps/size.m % Shadowed localtruncps method /MATLAB/toolbox/mbc/mbcmodels/@localusermod/size.m % Shadowed localusermod method /MATLAB/toolbox/mbc/mbcmodels/@xregmodel/size.m % Shadowed xregmodel method /MATLAB/toolbox/mbc/mbcmodels/@localsurface/size.m % Shadowed localsurface method /MATLAB/toolbox/mbc/mbcmodels/@xregtwostage/size.m % Shadowed xregtwostage method /MATLAB/toolbox/mbc/mbcmodels/@xregusermod/size.m % Shadowed xregusermod method /MATLAB/toolbox/mbc/mbcmodels/@xreglinear/size.m % Shadowed xreglinear method /MATLAB/toolbox/mbc/mbcmodels/@xregmulti/size.m % Shadowed xregmulti method /MATLAB/toolbox/mbc/mbcmodels/@localpspline/size.m % Shadowed localpspline method /MATLAB/toolbox/mbc/mbctools/@sweepset/size.m % Shadowed sweepset method /MATLAB/toolbox/mbc/mbctools/@xregmonitorplotproperties/size.m % Shadowed xregmonitorplotproperties method /MATLAB/toolbox/mbc/mbctools/@sweepsetfilter/size.m % Shadowed sweepsetfilter method /MATLAB/toolbox/mbc/mbctools/@xregdataset/size.m % Shadowed xregdataset method /MATLAB/toolbox/mpc/mpc/@explicitMPC/size.m % Shadowed explicitMPC method /MATLAB/toolbox/mpc/mpc/@mpc/size.m % Shadowed mpc method /MATLAB/toolbox/nnet/deep/deep/@dlarray/size.m % Shadowed dlarray method /MATLAB/toolbox/parallel/array/distributed/@codistributed/size.m % Shadowed codistributed method /MATLAB/toolbox/parallel/array/distributed/@distributed/size.m % Shadowed distributed method /MATLAB/toolbox/parallel/gpu/gpu/@gpuArray/size.m % Shadowed gpuArray method /MATLAB/toolbox/parallel/lang/@Composite/size.m % Shadowed Composite method /MATLAB/toolbox/robust/rctobsolete/robust/@icsignal/size.m % Shadowed icsignal method /MATLAB/toolbox/shared/controllib/engine/@InputOutputModel/size.m % Shadowed InputOutputModel method /MATLAB/toolbox/shared/statslib/@dataset/size.m % Shadowed dataset method built-in (/MATLAB/toolbox/matlab/elmat/size) % Shadowed
[row1, col1, s1] = ind2sub(size(chi_coeff), idx1);
Array indices must be positive integers or logical values.
.
  4 个评论
dpb
dpb 2025-7-26
编辑:dpb 2025-7-26
Good catch, @Star Strider. OP did well to have checked about ind2sub, but forgot or didn't think about size (nor did I after noticed he had checked the one, but it is a fairly common mistake).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by