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 日

0 投票

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)
dpb
dpb 2015 年 3 月 15 日
>> [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)
dpb
dpb 2015 年 3 月 16 日
I really don't follow what your description/nomenclature is driving at, sorry...the former was simply reproducing your results; nothing more.
Nnedoe
Nnedoe 2015 年 3 月 16 日
Its Okay dpb, I think the "extract" will do the trick

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 3 月 16 日

0 投票

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 ExchangeProgramming についてさらに検索

タグ

タグが未入力です。

質問済み:

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