Array Indexing Logical Values

1 回表示 (過去 30 日間)
Jacob Smith
Jacob Smith 2019 年 3 月 5 日
コメント済み: Jacob Smith 2019 年 3 月 5 日
My colleague and I were trying to iterate through an array using time steps. However, on certain time steps and time lengths, we recieve an "Array indexes must be positive indexes or logical values" error.
Why does this error appear at all?
Why does this error only appear sometimes?
We know that rounding the index prevents the error, but the index is already displayed as an integer, so why would that matter? Is it because the index (1e5) is really a decimal? (i.e. Is there a hidden decimal place not being shown to the user that makes the index not an integer?)
Any help is appreciated
Relevant Forum Posts
Code
disp("Notice how these parameters have no error");
stepThroughTime(.0001,3);
disp("While these parameters do");
stepThroughTime(.00001,3);
function stepThroughTime(steps,time)
%This function steps through an array of times
%bugs: certain steps and times result in error
times=0:steps:time;
windowsize=1/steps;
disp(windowsize);
for b=windowsize+1:windowsize:length(times)
x=b-windowsize;
%When x is rounded, there is no error
%x=round(x);
times(x);
end
end
  1 件のコメント
Stephen23
Stephen23 2019 年 3 月 5 日
編集済み: Stephen23 2019 年 3 月 5 日
"Why does this error appear at all?"
Because operations on floating point numbers accumulate floating point errors (which you did not take into account when you wrote your code).
"Why does this error only appear sometimes?"
Because sometimes the floating point errors accumulate in such a way that you get the same value that you would get from the equivalent symbolic or algebraic operations. But this should NOT be relied upon!
"We know that rounding the index prevents the error, but the index is already displayed as an integer, so why would that matter?"
Because displaying numeric data is a totally different thing to what numeric data is stored in memory. Do NOT confuse the two! Data is displayed as decimal numbers to a certain number of decimal places, but for most decimal fractions these are approximations of the actual binary values stored in memory.
"Is it because the index (1e5) is really a decimal?"
Yes.
"(i.e. Is there a hidden decimal place not being shown to the user that makes the index not an integer?)"
In fact those values cannot be stored as binary floating point numbers (in exactly the same way that you cannot write a finite expansion of 1/3 as a decimal fraction), and so your operations on them accumulate floating point error, thus your "index" is not an integer.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well:
PS: the solution is really simple: loop over indices of a data vector (rather than over the data values themselves).
PPS: although your title is "Array Indexing Logical Values" you are not actually using logical values anywhere in your code.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 3 月 5 日
編集済み: Walter Roberson 2019 年 3 月 5 日
100000
Array indices must be positive integers or logical values.
>> x
x =
100001
>> x - floor(x)
ans =
0.999999999985448
>> fprintf('%.99g\n', x)
100000.999999999985448084771633148193359375
The problem is that 1/0.00001 is not an integer because 0.00001 is not exactly representable in binary floating point.
  1 件のコメント
Jacob Smith
Jacob Smith 2019 年 3 月 5 日
Thank you for your responce, this helps a lot

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2019 年 3 月 5 日
"We know that rounding the index prevents the error"
So that's a big clue.
"Is there a hidden decimal place not being shown to the user that makes the index not an integer?"
Yes. Print out all of the digits and you will see it.
The solution is not to rely on floating point arithmetic to be exact in a decimal sense (neither 0.0001 nor 0.00001 can be represented exactly in IEEE double precision). You have to modify your algorithms to be robust for this.
  1 件のコメント
Jacob Smith
Jacob Smith 2019 年 3 月 5 日
Thank you for your responce, this help me understand the details more

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by