Can I just run some (loopless) code n times without having to modify the code to include a for loop?
Mostrar comentarios más antiguos
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;
load('F_points.mat');
load('fpep.mat');
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);
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];
% chord_dist = sqrt((chordx).^2 + (chordy).^2);
% adirector_dist = sqrt((adirector_x).^2 + (adirector_y).^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
Thanks in advance for your help!
3 comentarios
dpb
el 24 de Oct. de 2019
Well, hardly without any modifications, no, since the value you say you want to change is hardcoded into the script and it wipes anything outside it out when it starts every time.
"There is no free lunch!"
Daniel M
el 24 de Oct. de 2019
You're literally describing a loop. Don't try to take shortcuts when you know what the proper solution is. Turn your script into a function, and call it in a loop.
Steve
el 25 de Oct. de 2019
Respuestas (1)
Steven Lord
el 24 de Oct. de 2019
0 votos
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.
1 comentario
Steve
el 25 de Oct. de 2019
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!