Show loop calculations and iteration details in a matrix
Show older comments
I'm very new to programming, and I've done my best to find the answer to this on my own. I've got something, but I'm not sure if it will work (I can't test it out until Friday).
Basically, I've set up nested loops to filter through a set of data. The first loop selects all the rows where Column 1 = i (for i = 1:2). The second loop selects all the rows within that selection where Column 2 = j (for j = 1:3). The third loop does the same for Column 3 (for k = 1:5). I then perform some basic calculations on Column 4 for the final selection (mean and standard deviation).
What I want to do is save the results of those calculations, along with the iterations for each loop, in a matrix, so I would end up with something like this:
1 1 1 Mean SD
1 1 2 Mean SD
1 1 3 Mean SD...
2 3 5 Mean SD
The solution I've found is to define n = 0 and summary = cell(22,5) before I go into the loops. In the third loop, I then have this:
n = n + 1;
results_mean = mean(results_data);
results_SD = std(results_data);
results_summary = [i, j, k, results_mean, results_SD];
summary(n,:) = results_summary;
Will this work? If not, how else can I do it? I haven't been able to find a solution to this anywhere. If it does work, is there a more elegant solution that I've missed? Like I said, I'm very new to all this, so any help would be greatly appreciated!
Answers (1)
Sean de Wolski
on 7 Dec 2011
Looks like accumarray or grpstats would be your friend.
Perhaps you could post the whole code and a (small) sample matrix.
doc accumarray
doc grpstats
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!