How to loop through the table and assign 1's with an if statement
4 ビュー (過去 30 日間)
古いコメントを表示
Jessica Dawson
2020 年 7 月 29 日
コメント済み: Jessica Dawson
2020 年 7 月 29 日
I want to loop through dataFix (which is 8 columns wide and 39500 rows).
If there is a 1 in that cell in dataFix, and ALSO a 1 in the corresponding row of Speaking (which is a 1 column wide and 39500 rows long), I want dataSpeak to be a 1, if not 0.
dataSpeak is the same dimensions as dataFix.
At the moment, dataSpeak is just zero's and the 1's are not being correctly assigned.
Thank you!
dataSpeak=zeros(maxTime,nUP)
for l=1:length(dataFix)
if dataFix==1 & Speaking ==1;
dataSpeak=1;
end
end
0 件のコメント
採用された回答
Cristian Garcia Milan
2020 年 7 月 29 日
Can you use?
dataSpeak=zeros(maxTime,nUP)
for l=1:length(dataFix)
if dataFix(i)==1 & Speaking(i) ==1;
dataSpeak(i)=1;
end
end
dataSpeak variable needs another index due to it has 2 dimensions.
3 件のコメント
Cristian Garcia Milan
2020 年 7 月 29 日
Sorry for the i index, I used it because it is the typical I use haha.
If you want to go down the other 7 colums, you have to use another for-loop:
dataSpeak=zeros(maxTime,nUP);
for l=1:size(dataFix,1)
for m=1:size(dataFix,1)
if dataFix(l,m)==1 & Speaking(l,m) ==1;
dataSpeak(l)=1;
end
end
end
But one for-loop inside another one is quite-slow, so I suggest you to use:
dataSpeak=zeros(maxTime,nUP);
idx = dataFix==1 & Speaking ==1;
dataSpeak(idx) = 1;
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!