Display results in one matrix (nested for loops

Hello,
I am working on my final year project, a script that detects moods/emotions generated by audio files. I am currently stuck here:
for i=1:2
for j=4:10
for k=2:3
for l=1:2
for m=1:2
X=(i,j,k,l,m);
End, end, etc.
The reason for this is that each element i,j,k,l,m of the array has several values (i.e. i is 1 or 2, j is 4 through 10 etc.)
Now the problem: I get each possible array in a single line, is there any way to store all of them in a single matrix?
Thank you in advance!
M.

2 Comments

I guess it's
X(i,j,k,l,m)=someting
it won't be a bad idea to correct your code.
You said you want it all in one matrix! Well, they are in one matrix as far as I understand! X is a matrix and apparently that's where you wanna save different values!
Your question is vague!
No, because of the for loop, it displays each value in the same vector with 5 elements and it prints it out for every iteration of the loop. I am looking to collect these iterations in one big 5 by n matrix.
Sorry if I'm being vague, I'm trying to work on a train full of people...

Sign in to comment.

 Accepted Answer

Mihnea, you probably want
X(i,j,k,l,m) = ...
For large matrices you might want to pre-allocate memory:
X = zeros(2,10,3,2,2);

6 Comments

Hmm, it doesn't seem to work, says dimension mismatch. Any other ideas?
What exactly are you assigning to X? E.g., you could do (as an example)
X(i,j,k,l,m) = 10000*i + 1000*j + 100*k + 10*l + m;
What definitely does not work is something like you've shown
X(i,j,k,l,m) = (i,j,k,l,m);
I am trying to assign possible values of each variable to columns 1 to 5.
For example:
X = [1 4 2 1 1]
[1 4 2 1 2]
[1 4 2 2 1]
etc.
It bassically cycles through each variable value possible.
It does that nicely but it outpuuts one matrix with 5 columns at a time, I need it all in one big matrix.
I see. What you want is something like
pp = 1;
for i=1:2
for j=4:10
for k=2:3
for l=1:2
for m=1:2
X(pp,1:5) = [i j k l m];
pp = pp + 1;
end
end
end
end
end
Brilliant, thank you so much!
M.
Thanks Mischa Kim, so simply and beautifully resolved what I wanted:)
Have a beautiful day!!!!!!!!!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!