Filling out multiple structures in an if loop

1 Ansicht (letzte 30 Tage)
Tim Johansson
Tim Johansson am 16 Okt. 2020
Kommentiert: Ameer Hamza am 19 Okt. 2020
Im trying to set up some data to use in machine learning, but i am having a hard time setting up the data.
Right now im trying to sort my data into different structures according to label.
for k = 1:20
if rawData.sub1.run.label{1, 1}.data(k,3) == 0
classData.sub1.right{k}=rawData.sub1.run.epochs.run1.movement{k}
elseif rawData.sub1.run.label{1, 1}.data(k,3) == 1
classData.sub1.up{k}=rawData.sub1.run.epochs.run1.movement{k}
elseif rawData.sub1.run.label{1, 1}.data(k,3) == 2
classData.sub1.left{k}=rawData.sub1.run.epochs.run1.movement{k}
elseif rawData.sub1.run.label{1, 1}.data(k,3) == 3
classData.sub1.down{k}=rawData.sub1.run.epochs.run1.movement{k}
end
end
My problem is the "right{k}" ... down{k} structures.
if i dont input a k, the loop will simply replace the data that is already placed in the structure with the new data that belongs in the scructure.
However if i give the input k, multiple cells inside the structure is left as null, making it hard to call for the data again.
I've been reading up on structures but cant seem to find a simple solution.
i was thinking if it was possible to have a integer that only changed value every forth loop?
Any ideas are appreciated

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 16 Okt. 2020
If you want an integer which only increment every fourth iteration then you can use ceil() or floor() functions. For example
for k = 1:20
disp(ceil(k/4))
end
Result
>> for k = 1:20
disp(ceil(k/4))
end
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
5
5
5
5
  2 Kommentare
Tim Johansson
Tim Johansson am 19 Okt. 2020
Thank you very much!
Ameer Hamza
Ameer Hamza am 19 Okt. 2020
I am glad to be of help!!!

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by