How to loop through the table and assign 1's with an if statement

3 次查看(过去 30 天)
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

采纳的回答

Cristian Garcia Milan
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by