Issue t<0 in an for t=-5:10 loop.

5 ビュー (過去 30 日間)
NS
NS 2016 年 5 月 4 日
編集済み: Stephen23 2016 年 5 月 5 日
I'm running a code (attached) and the 'int' variable should take value 0 from t= -5:-1, and be 1 for all time periods after. I've tried putting it in my code in two different ways: specifying as "if t<0, int=0; else int=1" and "if t==-5:-1, int=0; else int=1;".
But for some reason I can see in my results that doing this Matlab ignores that before t=-1 int=0 and just takes 1 as the value for all time periods. Any idea how to solve this?
  3 件のコメント
NS
NS 2016 年 5 月 4 日
編集済み: NS 2016 年 5 月 4 日
Thanks for your feedback. I've just started with Matlab one week ago and am still getting the hang of it.
I've just added additional commentes to my code describing it and changed pi to inflation. However, would you have any tips on how to fix the t<0 issue potentially?
Stephen23
Stephen23 2016 年 5 月 4 日
編集済み: Stephen23 2016 年 5 月 5 日
@Nicole Stoelinga: as I explained in my answer, you are always comparing the same value of t, so you will ways get the same output for int. I have no idea what you are trying to do, or what the algorithm should be, because you didn't give this information and I don't read minds.
If t should change inside a loop, then move it inside the correct loop. Or use the vectorized version that I showed you. I can't tell you which is best for your algorithm because I am not in your mind.
Read the link at the bottom of my answer. Think about what you are trying to do. Understand what the code is actually doing.
Test every line as you write it. Read that again, and do this when you are writing your code. Really test it.
This means testing that it accepts the right inputs (and edge cases), and gives the expected outputs. You can check this, because you know the algorithm that you are using.
You see, I answered your question: "Any idea how to solve this?", where you wanted to know how to get the correct int values for different t values. It is now up to you to learn how to think about your algorithm, and where/how the t value should be defined.
Why not just use the code that I already gave you in my answer?
tVec = -5:10;
iVec = double(tVec>-1);
%
for k = 1:numel(tVec)
...
t = tVec(k);
int = iVec(k);
...
end
Or even
for t = -5:10
...
int = double(t>-1);
...
end

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

回答 (1 件)

Stephen23
Stephen23 2016 年 5 月 4 日
編集済み: Stephen23 2016 年 5 月 4 日
You don't need and if's or the like, the simplest way is to use a logical comparison and convert this to double:
int = double(t>-1)
which will work with scalar values, and also with vectors:
>> t = -5:5
t =
-5 -4 -3 -2 -1 0 1 2 3 4 5
>> int = double(t>-1)
int =
0 0 0 0 0 1 1 1 1 1 1
However it seems that you are getting confused about loops: by the time you code gets to the comparison on these lines:
if t<0
int=0;
else
int=1;
...
end
the loops have already ended, which means the value of t will always be whatever its final value was inside the loop, like this:
>> for t = -5:6, sqrt(t); end
>> t % last loop value
t =
6
After the loop t has the last value that it had inside the loop, yet you seem to believe that it should be changing. The problem is not the comparison, but your algorithm.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by