How can I write a code for subscripts indicating a past event?
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
Image Analyst
2015-3-16
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!