How do I populate the next term in an If/elseif code?

Here's my code:
N=[];
for i=1:length(timeStep);
if timeStep(i)>33;
N(i)=1;
elseif timeStep(i)<=33;
N(i)=0;
end
end
n=N';
n(1)=1;
If timeStep at i is greater than 33, I would like N of the NEXT term (i+1) to be populated with a 1. However, changing the code to N(i+1)=1 does not work.
What would make this work for me?
Thank you in advance.

 採用された回答

Michael Haderlein
Michael Haderlein 2015 年 3 月 11 日

0 投票

Why should it not work to change it to N(i+1)=1?
I'm wondering what's the desired output for timeStep between 35 and 40?
In any case, you can very simply vectorize the code:
N=[1 timeStep>40];

4 件のコメント

Alan Gallagher
Alan Gallagher 2015 年 3 月 11 日
Michael,
In a nutshell, timeStep represents a data point taken every 32 seconds. However, when the car is off, the timeStep is much larger than 32 seconds, but is only recorded once.
The timeStep vector looks something like ...32 32 32 32 500 32 32... so when it reads 500, i need the next value in N to be 1. In other words, the 1st 32 value after a larger number should have a 1 in the N vector to go with it.
Does this help?
Alan Gallagher
Alan Gallagher 2015 年 3 月 11 日
N acts as a "switch"
Michael Haderlein
Michael Haderlein 2015 年 3 月 11 日
I think that's what my code line is doing:
>> timeStep=[32 32 500 32 32 32 32 500 32 32];
>> N=[1 timeStep>40];
>> [timeStep;N(1:end-1)]
ans =
32 32 500 32 32 32 32 500 32 32 % <--timeStep
1 0 0 1 0 0 0 0 1 0 % <--N
% ^first after 500 ^first after 500
Here you can compare timeStep and N. Of course, N will have one more value, so I removed the last one for the comparison.
Alan Gallagher
Alan Gallagher 2015 年 3 月 11 日
Thanks Michael, this worked!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by