MATLAB Answers

Grouping calculated values of for loops together

1 view (last 30 days)
Timo Baumann
Timo Baumann on 14 Feb 2020
Edited: Timo Baumann on 14 Feb 2020
Hi Guys :)
I'd like to group calculated values of for loops together so I can create a boxplot of them.
Suggestions and help would be much appreciated, since I can't find a proper and easy way to do this.
Thank you in advance :)


Jon on 14 Feb 2020
It is not clear from your question what you are trying to do. Please include the code you have tried so far, and explain what input you have and what output you are hoping to obtain. Show if possible a simple example.
Timo Baumann
Timo Baumann on 14 Feb 2020
The following is just a simplified and very short example of what I'd like to do:
e = [0.1 0.2 0.8 0.9]
r = [0.9 1.2 2.8 7.9]
for i = length(e)
for d = 70:-1:0
I'd like to create a boxplot of "cs" and "b" and also be able to store every value of "b" and "cs" in a seperate vector, so I can use them in different plots with other values, that are calculated in a similar fashion in the bigger version of the code.
I was thinking maybe something like this could work:
e = [0.1 0.2 0.8 0.9]
r = [0.9 1.2 2.8 7.9]
for i = length(e)
for d = 70:-1:0
Unfortunatelly it didn't really do what I wanted it to (as stated above).
A table showing every different value of "e", "d" and "r" with the corresponding caclulated value of "cs" and "b" would also be part of what I hope to achieve.
T = table(e,r,cs,d,b);
Using "T = table(...)" didn't quite give me the desired table with all the variables and their respective calculated values in rows and columns and I'm not sure how that could work with nested for-loops (which I don't know how to avoid, since I want to test every value of every variable with one another).
I'm asking a lot at once here sry ^^
But thanks for helping me with at least some suggestions :)

Sign in to comment.

Accepted Answer

Guillaume on 14 Feb 2020
I'm not entirely clear on what you mean by boxplot nor what you're trying to achieve since none of the pieces of code you posted work. perhaps in the first one you meant to write
e = [0.1 0.2 0.8 0.9]
r = [0.9 1.2 2.8 7.9]
for i = length(e)
cs=e*2.93.*r; % .* instead of *
for d = 70:-1:0
b=cs.^2.*e+d; % .^ instead ^
which at least doesn't error even if it overwrites cs and b on each steps of the loop.
If you meant to create cs for each possible combination of e and r, and create b for each combinatio of e, r and d, then you don't need a loop:
%to create all combinations, the input vectors must each be in a different direction
e = [0.1; 0.2; 0.8; 0.9]; %column vector Mx1
r = [0.9, 1.2, 2.8, 7.9]; %row vector 1xN
d = permute(70:-1:0, [1, 3, 2]); %vector in 3rd dimension, 1x1xP
cs = 2.93 * e .* r; %2D matrix, MxN
b = cs.^2 .* e + d; %3D matrix, MxNxP
and to create the desired table:
[ee, rr, dd] = ndgrid(e, r, d);
table(ee(:), rr(:), cs(:), dd(:), b(:), 'VariableNames', {'e', 'r', 'cs', 'd', 'b'})
Note that If you were to use loops the code would go like this:
e = [0.1, 0.2, 0.8, 0.9]; % 1xM
r = [0.9, 1.2, 2.8, 7.9]; % 1xN
d = 70:-1:0; % 1xP
cs = zeros(numel(e), numel(r)); %preallocate MxN matrix
d = zeros(numel(e), numel(r), numel(d)); %preallocate MxNxP matrix
for eidx = 1:numel(e)
cs(eidx, :) = 2.93 * e(eidx) .* r;
for didx = 1:numel(d)
b(eidx, :, didx) = cs(eidx, :).^2 .* e(eidx) + d(didx);
Overall, much simpler without the loop.

  1 Comment

Timo Baumann
Timo Baumann on 14 Feb 2020
Thank you so much :D
That's exactly what I was looking for :)
Maybe one additional question:
When there are more than 2 input vectors (not just "e" and "r", but also "p","z" and"y") how do I have to write them to let them have different directions (as you mentioned in your answer).
When writing:
e = [0.1, 0.2, 0.8, 0.9];
r = [0.9, 1.2, 2.8, 7.9];
p = [0.7 1.4 2.9 9.9];
z = [0.1, 4.2, 1.8, 0.9];
y = [0.9 1.8 2.4 3.2];
It keeps giving me an error.
Sorry for beeing annoying. You've really helped me a lot already with your answer :)
(In one calculation I use e, r, p, z and y so I don't know how to make sure they are in different directions)

Sign in to comment.

More Answers (0)

Sign in to answer this question.