Grouping calculated values of for loops together

3 次查看(过去 30 天)
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 :)
  2 个评论
Jon
Jon 2020-2-14
编辑:Jon 2020-2-14
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 2020-2-14
编辑:Guillaume 2020-2-14
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)
cs=e*2.93*r;
for d = 70:-1:0
b=cs^2.*e+d;
end
end
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)
cs(i)=e*2.93*r;
for d = 70:-1:0
b(i)=cs(i).^2.*e(i)+d;
end
end
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);
disp(T)
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 :)

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2020-2-14
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 ^
end
end
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);
end
end
Overall, much simpler without the loop.
  1 个评论
Timo Baumann
Timo Baumann 2020-2-14
编辑:Timo Baumann 2020-2-14
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by