Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by