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?

 채택된 답변

Stephen23
Stephen23 2015년 3월 15일
편집: Stephen23 2015년 3월 15일
If you want to compare values with the previous values, why not use diff instead:
>> A = [1,1,0,1,0,0,0,1,1,1,0]
A =
1 1 0 1 0 0 0 1 1 1 0
>> B = [false,diff(A)]
B =
0 0 -1 1 -1 0 0 1 0 0 -1
>> q(numel(B)) = 0;
>> q(B==1) = 1 - 4/5;
>> q(B==0&A==1) = 1;
>> q
q =
1 1 0 0.2 0 0 0 0.2 1 1 0
You need to decide what value is appropriate for the first element.
This uses MATLAB's rather powerful logical indexing to create fully vectorized code which is faster and more expandable than using a loop. You should learn how vectorization and indexing works in MATLAB.

댓글 수: 5

Nnedoe
Nnedoe 2015년 3월 15일
편집: dpb 2015년 3월 15일
Hi Stephen, the diff function seems to subtract previous values of A from its current value. What I am asking for is a function or command that produces the actual previous value. Example:
A = 0 0 1 0 1 1 0 1 0 0 1
is there a way B can be
B = 0 0 0 1 0 1 1 0 1 0 0
So that it is the index of q that is subtracted. In other words, all values q(A-1) = all corresponding values of q(B)
>> [A; [B(2:end) A(end)]]
ans =
0 0 1 0 1 1 0 1 0 0 1
0 0 1 0 1 1 0 1 0 0 1
>>
Nnedoe
Nnedoe 2015년 3월 16일
편집: Nnedoe 2015년 3월 16일
dpb, I actually want to know how to generate B from A with a code.I want to use A and B as subscripts and I am assuming I do not know B yet. I think if I define A and B so they can have the same dimensions it may work,
>>A = 1:11;
>>B = 2:12;
In that case q(A) = q(B-1)
I really don't follow what your description/nomenclature is driving at, sorry...the former was simply reproducing your results; nothing more.
Its Okay dpb, I think the "extract" will do the trick

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
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에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 3월 15일

답변:

2015년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by