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

7 views (last 30 days)
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

Accepted Answer

Cristian Garcia Milan
Cristian Garcia Milan on 29 Jul 2020
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 Comments
Cristian Garcia Milan
Cristian Garcia Milan on 29 Jul 2020
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;

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!