How can I write a code for subscripts indicating a past event?
Ältere Kommentare anzeigen
If I have 6 intervals of a process and I want to compare a current interval to a previous interval, how can I do it? For example: I have a binary variable y that changes at the 6 intervals, and i want to write a conditional statement i can say:
i = 1:6;
if (y(i)==1 & y(i-1)==0)
q = 1-4/5;
else
if (y(i)==1 & y(i-1)==1)
q = 1;
end
end;
After writing something similar to this, Matlab gives the error message:
"Subscript indices must either be real positive integers or logicals".
This message appears because matlab has calculated i-1 for i = 1.
I have tried to use for loop, indicating values for i =1 and for i = 2:6 example:
for i = 1
y(i) = 0;
end
for i = 2:6
if (y(i)==1 & y(i-1)==0)
q = 1-4/5;
else
if (y(i)==1 & y(i-1)==1)
q = 1;
end
end
end;
In this case, its like matlab uses only i = 2:6 because I still receive an error message that the elements must match.
How can I fix this, please?
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 16 Mär. 2015
Instead of this
y(i)==1 & y(i-1)==0
Try this
y(i)==1 && y(i-1)==0
So you're using two ampersands.
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!