MATLAB Answers

Store values in an array from loop

11 views (last 30 days)
Alessandro Ruda
Alessandro Ruda on 24 Feb 2020
Commented: Bob Nbob on 25 Feb 2020
Hello everyone,
I have a vector HDA which is associated to different lists of angles.
For each list (a1, b1, ... etc.) i need to convert all the values in radians and make the circular average together with the standard
deviation with the functions Circ_ang2rad, circ_mean and circ_std.
Then I print the values. Clearly, I don't know how to make a proper loop so if anybody is willing to help I would be most grateful!
It would be also nice to print the result with a 'name' associated to it so that one could understand which result refers to which angle.
a1 = Asn42OH3p(:,1); %List of angles from traiectory for Asn42
b1 = Asp44O4p(:,1); %List of angles from traiectrory for Asp44
...and so on
HDA = [a1 b1 c1 d1 e1 f1 g1];
for X = HDA;
A_rad = circ_ang2rad(X);
A_bar = circ_mean(A_rad);
[s_A s0_A] = circ_std(A_rad);
fprintf('X torsion');
fprintf('\n')
fprintf('Mean resultant vector: \t%.2f \t%.2f\n', circ_rad2ang([A_bar]));
end

  0 Comments

Sign in to comment.

Accepted Answer

Bob Nbob
Bob Nbob on 24 Feb 2020
I'm not sure what you mean by printing the 'name' associated to a data set, but there are my modifications to what you have setup.
a1 = Asn42OH3p(:,1); %List of angles from trajectory for Asn42
b1 = Asp44O4p(:,1); %List of angles from trajectrory for Asp44
...and so on
HDA = [a1 b1 c1 d1 e1 f1 g1];
A_rad = circ_ang2rad(HDA); % Can remove this from the loop, as I believe the function operates on all
% elements in the input array individually.
for X = 1:size(A_rad,2); % Work through all columns of A_rad, works with one data set per loop.
A_bar(X) = circ_mean(A_rad(:,X)); % Find average for current data set. Store in array
[s_A(:,X) s0_A(:,X)] = circ_std(A_rad(:,X)); % Standard deviation for each data set.
% You might need to adjust the indexing if the outputs above (I don't know that they're all arrays)
fprintf('X torsion');
fprintf('\n')
fprintf('Mean resultant vector: \t%.2f \t%.2f\n', circ_rad2ang([A_bar]));
end

  2 Comments

Alessandro Ruda
Alessandro Ruda on 24 Feb 2020
Thank you Bob! It worked fine!
I want to associate to each result a string to make clear what that value is referring to, so i want to associate an array like the one below to the results that i get from the loop!
HDA_names = [Asn42_OH3p Asp44_O3p Lys74_OH4p Asn42_O3pp Ser52_OH3pp Asn53_OH4pp Thr57_O4pp];
so that i can have for instance:
Asn42_OH3p
Mean resultant vector: 158.23
Standard deviation: 0.15
and so on...
Bob Nbob
Bob Nbob on 25 Feb 2020
Sure. If you establish a variable like HDA_names (I recommend doing a cell or string array, rather than a concat like that) then you should just be able to use the same index setup and include one more fprintf line in your current loop.
HDA_names = {'Asn42_OH3p' 'Asp44_O3p' 'Lys74_OH4p' ... };
% Rest same as before
for X = ...
% Again same as before
% Include this next line as the first of your fprintf lines
fprintf('\c',HDA_names{X}) % There is a way to underline the output, I believe,
% but I don't remember it off the top of my head.
% I recommend looking up fprintf for more details
end

Sign in to comment.

More Answers (0)