IF statement problem in creating an array with zeros

for t=1:1441
if 960<t<1150
EV1_6=Ci;
else
EV1_6=0;
end
I am trying to create a new array EV1_6 that will have the values from Ci while in the range of 960 to 1150 and fill the rest of the array with zeros. I need the zeros as this will be used for another task that simulates voltage.

回答 (1 件)

the cyclist
the cyclist 2017 年 7 月 17 日
編集済み: the cyclist 2017 年 7 月 17 日

1 投票

if 960<t<1150
is not proper syntax for an if statement. You need
if 960<t && t<1150
See the documentation for details.
You also don't need a for loop:
EV1_6 = zeros(1,1441);
EV1_6(961:1149) = Ci;
will do the same thing, more efficiently.

4 件のコメント

Dave Griffin
Dave Griffin 2017 年 7 月 17 日
編集済み: Dave Griffin 2017 年 7 月 17 日
when I try
EV1_6 = zeros(1,1441); EV1_6(961:1149) = Ci;
it results in an error "In an assignment A(:) = B, the number of elements in A and B must be the same."
the cyclist
the cyclist 2017 年 7 月 17 日
What is the size of Ci? From your prior syntax, I assumed it was a scalar.
Dave Griffin
Dave Griffin 2017 年 7 月 17 日
Ci is 1441x1 double
the cyclist
the cyclist 2017 年 7 月 17 日
編集済み: the cyclist 2017 年 7 月 17 日
OK, so do you mean that you want EV1_6 to be zero, except for the elements in that particular range, which should be copied over from Ci? In that case,
EV1_6 = zeros(1441,1);
EV1_6(961:1149) = Ci(961:1149);

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

カテゴリ

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

質問済み:

2017 年 7 月 17 日

編集済み:

2017 年 7 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by