Index out of bounds using "length"

1 次查看(过去 30 天)
Christine Fesler
Christine Fesler 2015-12-29
编辑: dpb 2015-12-31
Hi, I have a loop that references a 1x82 data structure. Each matrix, "CF(i).SincIncep", has a different number of rows but all have 4 columns; therefore, I am using the length command for each "i" in order to get the unique number of rows for the calculation. The problem is, I am getting an index out of bounds error which I do not understand as the actual matrix referenced in the error is stored as a 3x4 double. The length command results in the correct length for the majority of the calculations in the loop, but not for the one mentioned, which says the length is 4. I am not sure why the "length" command would state there are 4 rows when there are only 3 rows in the matrix. I tried using "size" instead; however, it does not make sense to me why the "length" command would work correctly for some and not for others as all matrices are mx4. The loop I am using is below along with the error output:
for i = 1:82;
for n = 1:length(CF(i).SincInc);
for j = 2;
CF(i).SincInc(n,j+2) = CF(i).SincInc(n,j) + CF(i).SincInc(n,j+1);
end
end
end
Attempted to access CF.SincInc(4,2); index out of bounds because size(CF.SincInc)=[3,4].

回答(2 个)

Star Strider
Star Strider 2015-12-29
The length command will return the value of the largest dimension. If you want to be certain to only return the number of rows, use the size function, specifically size(x,1):
for n = 1:size(CF(i).SincInc, 1);

dpb
dpb 2015-12-29
编辑:dpb 2015-12-29
WAD.
length Length of vector.
length(X) returns the length of vector X. It is equivalent
to MAX(SIZE(X)) for non-empty arrays...
Note you've got an array size(3,4) --> max(3,4) ==> 4. Hence, length --> 4, not 3.
It's an unfortunate "feature" in some ways that length isn't equivalent to size(x,1) for 2D arrays but that's "just the way it is". Note also that the help says it is length of vector X; which is a 1D array, not 2D.
The moral of the story is if you want size(array,1), use that expression instead of length. It's far safer even if needs another argument in the calling expression.
  5 个评论
Walter Roberson
Walter Roberson 2015-12-31
However, using nrows and ncols would require that users pay attention to the orientation of their vectors, which is often not something that needs to be thought about.
dpb
dpb 2015-12-31
编辑:dpb 2015-12-31
There's that, too, which is undoubtedly where the definition came from as is implied by the help files. It's when applied to other than vectors it becomes an issue so maybe it should stay but perhaps evolve to only work for vectors in its new guise...or mayhaps have differing behavior of
if isvector(x)
len=max(size(x)); % vector length if is vector
else
len=size(x,1); % anything other than vector use row direction
end
Not that it's likely to change; too much existing baggage.

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by