# Can I just run some (loopless) code n times without having to modify the code to include a for loop?

2 views (last 30 days)
Steve on 24 Oct 2019
Commented: Steve on 25 Oct 2019
I would like to know if it is possible to run the attached code n times, without having to modify the code (i.e., to include a for loop); yet, every time the code is run, I need the input value (pt) to increment from 1 to n. I also need to store the output variables of the code within in a single variable that contains the output from all n iterations (please see attached code):
close all;
clearvars;
xy = F_points';
pt = 299; % choose a point in xy and we'll find the n nearest neighbors.
% Euclidean distance between xy(p,:) and all other points
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
n = 3; % Find the n closest values (excluding the point selected)
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
xyNearest = xy(ascendIdx(1:n),:);
% dist_a = sqrt((fpep(pt,1)-fpep(pt,7)).^2 + (fpep(pt,2)-fpep(pt,8)).^2);
% dist_b = sqrt((fpep(pt,3)-fpep(pt,7)).^2 + (fpep(pt,4)-fpep(pt,8)).^2);
% dist_c = sqrt((fpep(pt,5)-fpep(pt,7)).^2 + (fpep(pt,6)-fpep(pt,8)).^2);
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_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];
% chord_dist = sqrt((chordx).^2 + (chordy).^2);
% bdirector_dist = sqrt((bdirector_x).^2 + (bdirector_y).^2);
% cdirector_dist = sqrt((cdirector_x).^2 + (cdirector_y).^2);
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,:)));
end
ThetaInDegrees_a = acosd(CosTheta_a);
ThetaInDegrees_b = acosd(CosTheta_b);
ThetaInDegrees_c = acosd(CosTheta_c);
% Plot
figure()
for ii = 1 : length(chordx)
xvals = [xy(pt,2),xy(pt,2)+chordy(ii)];
yvals = [xy(pt,1),xy(pt,1)+chordx(ii)];
plot(xvals,yvals,'r-')
hold on
end
plot(xy(:,2),xy(:,1),'b.')
hold on
% Show the selected point
plot(xy(pt,2),xy(pt,1),'b.','MarkerFaceColor', 'y')
% Show nearest 'n' dots
plot(xyNearest(:,2),xyNearest(:,1),'ro')
plot(fpep(:,2),fpep(:,1),'k.')
plot(fpep(:,4),fpep(:,3),'k.')
plot(fpep(:,6),fpep(:,5),'k.')
axis equal
Steve on 25 Oct 2019
Thanks to everyone for your feedback!
I can run my code (as shown above) with a specific input value for pt, and get exactly the output that I need for that particular pt. However, because I have 953 different points (values for pt), it would not be much fun to manually increment the pt (from 1 to 953) and run the code for each case. I did attempt (for many hours, but to no avail) to incorporate for loops within my code but the syntax became very busy, very fast--and I simply could not get it to work (see attached code below). Can anyone show me how this should be done?
Also, I read through the help section on converting a script into a function, but it's not clear to me how to actually do this with my script (above). Also, I'm not sure how to pass an input (pt) and how to pass the incremented outputs (ThetaInDegrees_a, ThetaInDegrees_b, and ThetaInDegrees_c) to the function, once it is defined. Can someone show me how to do this? Again, thanks for all your help!
close all;
clearvars;
xy = F_points';
for pt = 1 : 953 % iterate the point in xy and we'll find the 10 nearest neighbors.
% Euclidean distance between xy(p,:) and all other points
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
% Find the n closest values (excluding the point selected)
n = 3;
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
xyNearest = xy(ascendIdx(1:n),:);
% dist_a = sqrt((fpep(pt,1)-fpep(pt,7)).^2 + (fpep(pt,2)-fpep(pt,8)).^2);
% dist_b = sqrt((fpep(pt,3)-fpep(pt,7)).^2 + (fpep(pt,4)-fpep(pt,8)).^2);
% dist_c = sqrt((fpep(pt,5)-fpep(pt,7)).^2 + (fpep(pt,6)-fpep(pt,8)).^2);
bdirectorx(pt) = ([fpep(pt,3) - fpep(pt,7)]);
bdirector_x = bdirectorx';
bdirectory(pt) = ([fpep(pt,4) - fpep(pt,8)]);
bdirector_y = bdirectory';
cdirectorx(pt) = ([fpep(pt,5) - fpep(pt,7)]);
cdirector_x = cdirectorx';
cdirectory(pt) = ([fpep(pt,6) - fpep(pt,8)]);
cdirector_y = cdirectory';
vector_b = [bdirector_x bdirector_y];
vector_c = [cdirector_x cdirector_y];
chordx(pt,:) = ([xyNearest(:,1) - xy(pt,1)]);
chordy(pt,:) = ([xyNearest(:,2) - xy(pt,2)]);
% chord_dist = sqrt((chordx).^2 + (chordy).^2);
chordxy = [chordx chordy];
% for t = 1 : n
CosTheta_a(pt) = dot(vector_c(pt:pt,1:2),chordxy(pt:pt,1:2))/(norm(vector_c(pt:pt,1:2))*norm(chordxy(pt:pt,1:2)));
CosTheta_b(pt) = dot(vector_c(pt:pt,1:2),chordxy(pt:pt,3:4))/(norm(vector_c(pt:pt,1:2))*norm(chordxy(pt:pt,4:5)));
CosTheta_c(pt) = dot(vector_c(pt:pt,1:2),chordxy(pt:pt,5:6))/(norm(vector_c(pt:pt,1:2))*norm(chordxy(pt:pt,5:6)));
% end
end
% bdirector_dist = sqrt((bdirector_x).^2 + (bdirector_y).^2);
% cdirector_dist = sqrt((cdirector_x).^2 + (cdirector_y).^2);
% for t = 1 : n
% CosTheta_a(t) = dot(vector_a(1,:),chordxy(t:t,1:2))/(norm(vector_a(1,:))*norm(chordxy(t:t,1:2)));
% CosTheta_b(t) = dot(vector_b(1,:),chordxy(t:t,3:4))/(norm(vector_b(1,2))*norm(chordxy(t:t,4:5)));
% CosTheta_c(t) = dot(vector_c(1,:),chordxy(t:t,5:6))/(norm(vector_c(1,2))*norm(chordxy(t:t,5:6)));
% end
ThetaInDegrees_a = acosd(CosTheta_a);
ThetaInDegrees_b = acosd(CosTheta_b);
ThetaInDegrees_c = acosd(CosTheta_c);
% Plot
figure()
for ii = 1 : length(chordx)
xvals = [xy(pt,2),xy(pt,2)+chordy(ii)];
yvals = [xy(pt,1),xy(pt,1)+chordx(ii)];
plot(xvals,yvals,'r-')
hold on
end
plot(xy(:,2),xy(:,1),'b.')
hold on
% Show the selected point
plot(xy(pt,2),xy(pt,1),'b.','MarkerFaceColor', 'y')
% Show nearest 'n' dots
plot(xyNearest(:,2),xyNearest(:,1),'ro')
plot(fpep(:,2),fpep(:,1),'k.')
plot(fpep(:,4),fpep(:,3),'k.')
plot(fpep(:,6),fpep(:,5),'k.')
axis equal

Steven Lord on 24 Oct 2019
Technically yes, but practically you're probably going to want to just add the loop or convert your code into a function and call that function in a loop as Daniel M suggested.
Steve on 25 Oct 2019

R2019a

### Community Treasure Hunt

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

Start Hunting!