Index in position 2 exceeds array bounds

Hello,
i'm trying to add values to an already existing array (Profile_y). The last value of Profile_x is lager than the last value of Profile_y. I am calculating the difference and divide it by the average step size of the values to get the number of how many steps i need to add. After that i try to add the average stepsize MissingSteps-times to the vector, so that Profile_x and Profile_y should have the same endvalue after all.
Instead i'm getting the error:
Index in position 2 exceeds array bounds
The end operator must be used within an array index expression.
My code:
MissingSteps = floor((Profile_x(1,end) - Profile_y(1,end)) ./ (mean(diff(Profile_y))));
Profile_Y_new = [Profile_Y];
% Profile_Y_new(1,end+1:numel(Profile_Y_new) + MissingSteps) = 0;
k = 1;
for i = 1:MissingSteps
Profile_Y_new(1,end+k) = Profile_Y_new(1,end+k-1) + (mean(diff(Profile_Y)));
while k <= MissingSteps
k = k + 1;
end
end

8 件のコメント

LM
LM 2021 年 2 月 15 日
I have encountered this special error message many times before and still don't know, what is wrong with my general approach to loops.
Walter Roberson
Walter Roberson 2021 年 2 月 15 日
Which line is the problem occuring on?
What is class(Profile_x) and class(Profile_y) ?
while k <= MissingSteps
k = k + 1;
end
What is the point of that? Why not just do
k = MissingSteps + 1;
LM
LM 2021 年 2 月 15 日
I guess you want to know that Profile_x is a 1x100 double and Profile_y is a 1x9000 double.
The line with the error:
Profile_Y_new(1,end+k) = Profile_Y_new(1,end+k-1) + (mean(diff(Profile_Y)));
I am trying to let k go from 1 to MissingSteps. If it reaches MissingSteps it should stop.
Walter Roberson
Walter Roberson 2021 年 2 月 15 日
MissingSteps = floor((Profile_x(1,end) - Profile_y(1,end)) ./ (mean(diff(Profile_y))));
Profile_Y_new = [Profile_Y];
% Profile_Y_new(1,end+1:numel(Profile_Y_new) + MissingSteps) = 0;
k = 1;
for i = 1:MissingSteps
Profile_Y_new(1,end+k) = Profile_Y_new(1,end+k-1) + (mean(diff(Profile_Y)));
if k <= MissingSteps
k = k + 1;
else
break; %stop when enough has been filled
end
end
Walter Roberson
Walter Roberson 2021 年 2 月 15 日
Use the command
dbstop if error
and run the code. When it stops at that line, please ask it to display
class(Profile_Y_new)
class(Profile_Y)
which end
which mean
which diff
LM
LM 2021 年 2 月 15 日
編集済み: LM 2021 年 2 月 15 日
I have changed the code as you wrote. It still occurs the same error in the same line. k = 2 in the workspace.
When i use dbstop if error nothing happens.
I am not sure, if i have done this correctly in the command window, but:
class(Profile_Y_new) = 'double'
class(Profile_Y) = 'double'
K>> which end
built-in (C:\Program Files (x86)\toolbox\matlab\lang\end)
K>> which mean
C:\Program Files (x86)\toolbox\matlab\datafun\mean.m
K>> which diff
built-in (C:\Program Files (x86)\toolbox\matlab\datafun\@char\diff) % char method
Walter Roberson
Walter Roberson 2021 年 2 月 15 日
Those look okay.
When you are stopped at the error line, please command
Profile_Y_new(1,end+k-1)
(mean(diff(Profile_Y)))
Profile_Y_new(1,end+k) = 12345
and see where the error comes up. If it does not come up at all, try putting in the full line
Profile_Y_new(1,end+k) = Profile_Y_new(1,end+k-1) + (mean(diff(Profile_Y)));
and seeing what happens.
I'm wondering whether you have a syntax error in your program somewhere, an extra end statement, a [ opened that is not closed, something like that.
LM
LM 2021 年 2 月 15 日
編集済み: LM 2021 年 2 月 15 日
Typing in:
Profile_Y_new(1,end+k-1):
Error: Index in position 2 exceeds array bounds
(mean(diff(Profile_Y))):
Give the correct stepsize.
Profile_Y_new(1,end+k) = 12345:
Add the value at third position after the initial endvalue.
Matlab kind of have a problem with '+k'. How do I solve this?

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

 採用された回答

Jan
Jan 2021 年 2 月 15 日
編集済み: Jan 2021 年 2 月 15 日

0 投票

Profile_Y_new(1,end+k) = Profile_Y_new(1,end+k-1) + (mean(diff(Profile_Y)));
This line must fail, because Profile_Y_new(1,end) is the last element of Profile_Y_new already. Then Profile_Y_new(1,end+k-1) tries to read an element behind the last element.
Remember that " end " is an abbreviation for the length of the concerned dimension. Then reading the element at end+k must fail for k > 0.
I assume you mean:
endIndex = size(Profile_Y_new, 2);
for i = 1:MissingSteps
Profile_Y_new(1,endIndex + k) = Profile_Y_new(1, endIndex + k - 1) + ...
(mean(diff(Profile_Y)));
Now endIndex is fixed before the loop, while the keyword "end" is adjusted dynamically to the current size.

1 件のコメント

LM
LM 2021 年 2 月 17 日
Thanks a lot for your help and explanation. The problem is clear now.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

LM
2021 年 2 月 15 日

コメント済み:

LM
2021 年 2 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by