How do I store the changing values of a variable (generated in a for loop) into a single file without overwriting the previous one?

For example:
n = 100;
for ii = 1 : n
areatriangle = .5*((ii/2)*ii);
end
In the case above, I want to store all the outcomes/iterations of areatriangle, not just the last one. How do I do this?
Thanks in advance for your help!

 Accepted Answer

n = 100;
areatriangle = zeros(1,n)
for ii = 1 : n
areatriangle(ii) = .5*((ii/2)*ii);
end
or:
areatriangle = .5/2*(1:n).^2;

7 Comments

Thanks Alex. In my actual code (see below), I can't seem to get this to work for the output variables (i.e., ThetaInDegrees_a, ThetaInDegrees_b, ThetaInDegrees_c), where I need 10 angles for each pt (10 x 953). Can you help with me this? Thanks in advance for any help you could offfer.
close all;
clearvars;
load('F_points.mat');
load('fpep.mat');
xy = F_points';
n = 10; % Find the n closest values (excluding the point selected)
for pt = 1 : 953
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
xyNearest = xy(ascendIdx(1:n),:);
adirector_x = ([fpep(pt,1) - fpep(pt,7)]);
adirector_y = ([fpep(pt,2) - fpep(pt,8)]);
bdirector_x = ([fpep(pt,3) - fpep(pt,7)]);
bdirector_y = ([fpep(pt,4) - fpep(pt,8)]);
cdirector_x = ([fpep(pt,5) - fpep(pt,7)]);
cdirector_y = ([fpep(pt,6) - fpep(pt,8)]);
vector_a = [adirector_x adirector_y];
vector_b = [bdirector_x bdirector_y];
vector_c = [cdirector_x cdirector_y];
chordx = ([xyNearest(:,1) - xy(pt,1)]);
chordy = ([xyNearest(:,2) - xy(pt,2)]);
chordxy = [chordx chordy];
for j = 1 : n
CosTheta_a(j) = dot(vector_a,chordxy(j,:))/(norm(vector_a)*norm(chordxy(j,:)));
CosTheta_b(j) = dot(vector_b,chordxy(j,:))/(norm(vector_b)*norm(chordxy(j,:)));
CosTheta_c(j) = dot(vector_c,chordxy(j,:))/(norm(vector_c)*norm(chordxy(j,:)));
ThetaInDegrees_a(j) = acosd(CosTheta_a(j));
ThetaInDegrees_b(j) = acosd(CosTheta_b(j));
ThetaInDegrees_c(j) = acosd(CosTheta_c(j));
end
end
In this case you need to store the result in a matrix
for j = 1 : n
CosTheta_a(j,pt) = dot(vector_a,chordxy(j,:))/(norm(vector_a)*norm(chordxy(j,:)));
CosTheta_b(j,pt) = dot(vector_b,chordxy(j,:))/(norm(vector_b)*norm(chordxy(j,:)));
CosTheta_c(j,pt) = dot(vector_c,chordxy(j,:))/(norm(vector_c)*norm(chordxy(j,:)));
ThetaInDegrees_a(j,pt) = acosd(CosTheta_a(j));
ThetaInDegrees_b(j,pt) = acosd(CosTheta_b(j));
ThetaInDegrees_c(j,pt) = acosd(CosTheta_c(j));
end
Don't forget to initialize every variable before the loops to increase the speed:
CosTheta_a = zeros(n,953)
%And so on
Thanks again Alex. I tried running this change and it just gave me the first 10 angles 953 times. Any idea on what's going wrong? Thanks again!
It works for me! Where are you initializing the variables?
close all;
clearvars;
load('F_points.mat');
load('fpep.mat');
xy = F_points';
n = 10; % Find the n closest values (excluding the point selected)
CosTheta_a = zeros(n,953);
CosTheta_b = zeros(n,953);
CosTheta_c = zeros(n,953);
ThetaInDegrees_a = zeros(n,953);
ThetaInDegrees_b = zeros(n,953);
ThetaInDegrees_c = zeros(n,953);
for pt = 1 : 953
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
xyNearest = xy(ascendIdx(1:n),:);
adirector_x = ([fpep(pt,1) - fpep(pt,7)]);
adirector_y = ([fpep(pt,2) - fpep(pt,8)]);
bdirector_x = ([fpep(pt,3) - fpep(pt,7)]);
bdirector_y = ([fpep(pt,4) - fpep(pt,8)]);
cdirector_x = ([fpep(pt,5) - fpep(pt,7)]);
cdirector_y = ([fpep(pt,6) - fpep(pt,8)]);
vector_a = [adirector_x adirector_y];
vector_b = [bdirector_x bdirector_y];
vector_c = [cdirector_x cdirector_y];
chordx = ([xyNearest(:,1) - xy(pt,1)]);
chordy = ([xyNearest(:,2) - xy(pt,2)]);
chordxy = [chordx chordy];
for j = 1 : n
CosTheta_a(j,pt) = dot(vector_a,chordxy(j,:))/(norm(vector_a)*norm(chordxy(j,:)));
CosTheta_b(j,pt) = dot(vector_b,chordxy(j,:))/(norm(vector_b)*norm(chordxy(j,:)));
CosTheta_c(j,pt) = dot(vector_c,chordxy(j,:))/(norm(vector_c)*norm(chordxy(j,:)));
ThetaInDegrees_a(j,pt) = acosd(CosTheta_a(j));
ThetaInDegrees_b(j,pt) = acosd(CosTheta_b(j));
ThetaInDegrees_c(j,pt) = acosd(CosTheta_c(j));
end
end
I just copied your code and ran it; this is what I got for ThetaInDegrees_a (see screenshot):
ThetaInDegrees_a_screenshot.JPG
Ohhh yes, you need to change the following lines in the last loop:
ThetaInDegrees_a(j,pt) = acosd(CosTheta_a(j,pt));
ThetaInDegrees_b(j,pt) = acosd(CosTheta_b(j,pt));
ThetaInDegrees_c(j,pt) = acosd(CosTheta_c(j,pt));

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 30 Oct 2019

Commented:

on 30 Oct 2019

Community Treasure Hunt

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

Start Hunting!