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?

 Akzeptierte Antwort

Stephen23
Stephen23 am 15 Mär. 2015
Bearbeitet: Stephen23 am 15 Mär. 2015
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 Kommentare

Nnedoe
Nnedoe am 15 Mär. 2015
Bearbeitet: dpb am 15 Mär. 2015
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 am 16 Mär. 2015
Bearbeitet: Nnedoe am 16 Mär. 2015
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
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

Tags

Noch keine Tags eingegeben.

Gefragt:

am 15 Mär. 2015

Beantwortet:

am 16 Mär. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by