Why is the if statement not working?

2 ビュー (過去 30 日間)
tash7827
tash7827 2015 年 8 月 3 日
回答済み: the cyclist 2015 年 8 月 3 日
this is my code - it is supposed to be a binary array, where the length of time (through the array) that the "1"s are held is determined by "time" - why is the if statement not working based off of the output of "time"?
function cube=cube(exp)
exp=zeros(60,96,10);
time=randi([3 7]);
if time==3
for j=1:size(exp,1);
for jj=1:size(exp,2);
rand=randi([1 8]);
exp(j,jj,rand:rand+2)=1;
end
end
elseif time == 4
for j=1:size(exp,1);
for jj=1:size(exp,2);
rand=randi([1 7]);
exp(j,jj,rand:rand+3)=1;
end
end
elseif time == 5
for j=1:s ize(exp,1);
for jj=1:size(exp,2);
rand=randi([1 6]);
exp(j,jj,rand:rand+4)=1;
end
end
elseif time == 6
for j=1:size(exp,1);
for jj=1:size(exp,2);
rand=randi([1 5]);
exp(j,jj,rand:rand+5)=1;
end
end
elseif time == 7
for j=1:size(exp,1);
for jj=1:size(exp,2);
rand=randi([1 4]);
exp(j,jj,rand:rand+6)=1;
end
end
cube=exp
end
  1 件のコメント
the cyclist
the cyclist 2015 年 8 月 3 日
One suggestion, aside from trying to solve your main problem, is that you should not be using MATLAB keywords as variable names, because the code may behave in unexpected ways, and also the code simply becomes very confusing (especially for veteran users). In your case, exp and rand should be avoided as variable names.

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

採用された回答

the cyclist
the cyclist 2015 年 8 月 3 日
One problem is that in the elseif statement where you check if time == 5, you have
for j=1:s ize(exp,1);
instead of
for j=1:size(exp,1);

その他の回答 (1 件)

the cyclist
the cyclist 2015 年 8 月 3 日
編集済み: the cyclist 2015 年 8 月 3 日
You need
time==3
instead of
time=3
== is the "determine equality" operator. = is the assignment statement.
  1 件のコメント
tash7827
tash7827 2015 年 8 月 3 日
sorry -- i hadn't realized this was old code - i already have what you suggested and it still is not working - my whole array is still all zero.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by