Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

error subscript indices integers or logicals

1 Ansicht (letzte 30 Tage)
Payjay
Payjay am 20 Okt. 2016
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
for this code i get the error shown above, why? in particular it is for l = numel(((wechsel_X_idx(v):wechsel_X_idx(v+1))/2)-1); Y(l) = meaned_Y; i dont get it because when i try it with numbers without a for loop it works.. thanks anybody who can help!
if true
wechsel_X = diff(wertepaare(:,2));
wechsel_X_idx = find(wechsel_X);
for v = 1:(numel(wechsel_X_idx)-1)
meaned_Y = mean(Y(wechsel_X_idx(v)+1:wechsel_X_idx(v+1)));
Y(wechsel_X_idx(v):wechsel_X_idx(v+1)) = 0;
l = numel(((wechsel_X_idx(v):wechsel_X_idx(v+1))/2)-1);
Y(l) = meaned_Y;
end end
  1 Kommentar
Alexandra Harkai
Alexandra Harkai am 20 Okt. 2016
Which v value are you getting the error for? (Seems unlikely that line would cause the problem since the same indexing works in the line before.)

Antworten (1)

Guillaume
Guillaume am 20 Okt. 2016
Without knowing the content of the variables, it's difficult for us to diagnose the problem. However, the line:
l = numel(((wechsel_X_idx(v):wechsel_X_idx(v+1))/2)-1);
is very suspicious. You're asking with numel how many elements there are in a vector. That vector is made of the values wechsel_X_idx(v):wechsel_X_idx(v+1). That you're dividing the content of the vector by 2 and then subtracting 1 to the elements is not going to change the number of elements, so your line is exactly the same as:
l = numel(wechsel_X_idx(v):wechsel_X_idx(v+1));
which would be obtained more simply and explicitly as:
l = wechsel_X_idx(v+1) - wechsel_X_idx(v) + 1; %note that wechsel_X_idx(v+1) is guaranteed to be greater than wechsel_X_idx(v)
So is the line doing what you want?
Note that the best way for you to find where the problem lies in your code is to debug it, executing each line step by step and inspecting the contents of the variable. It should quickly become obvious when the content deviates from your expectations.
  1 Kommentar
Payjay
Payjay am 22 Okt. 2016
you are of course right with the "numel-thing", thank you! and no it didnt work as i wanted! now it works, think i mixed up some loops in an inappropriate way. and ur hint with the numel thing was also good, anyway thanks! :)

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by