Effacer les filtres
Effacer les filtres

Index in position 2 exceeds array bounds

1 vue (au cours des 30 derniers jours)
LM
LM le 15 Fév 2021
Commenté : LM le 17 Fév 2021
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 commentaires
Walter Roberson
Walter Roberson le 15 Fév 2021
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 le 15 Fév 2021
Modifié(e) : LM le 15 Fév 2021
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?

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 15 Fév 2021
Modifié(e) : Jan le 15 Fév 2021
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 commentaire
LM
LM le 17 Fév 2021
Thanks a lot for your help and explanation. The problem is clear now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by