Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Cristian Garcia Milan
Cristian Garcia Milan le 29 Juil 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 commentaires
Cristian Garcia Milan
Cristian Garcia Milan le 29 Juil 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;
Jessica Dawson
Jessica Dawson le 29 Juil 2020
This looks great and this is much simpler! Thank you so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by