Can someone help us with choosing the best syntax and nomenclature for this code?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I have this code we are trying to run to create vectors from n closest points. However, it seems to be getting too complicated. Can someone help us to have a cleaner, more efficient code given the attched file (fpep.mat)? Below is the current code. Thanks in advance for your help.
close all;
clearvars;
% load('F_points.mat');
load('fpep.mat');
n = 10;
for pt = 1 : 953
dist = sqrt((fpep(:,7)-fpep(pt,7)).^2 + (fpep(:,8)-fpep(pt,8)).^2);
[~, ascendIdx] = sort(dist); % Not really sure what this does or why there is a ~ used here. Can someone explain this?
xyNearest(pt,:,:) = fpep(ascendIdx(1:n),7:8);
adirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),1) - xyNearest(pt,:,1)]);
adirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),2) - xyNearest(pt,:,2)]);
bdirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),3) - xyNearest(pt,:,1)]);
bdirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),4) - xyNearest(pt,:,2)]);
cdirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),5) - xyNearest(pt,:,1)]);
cdirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),6) - xyNearest(pt,:,2)]);
ascendIdx(ascendIdx==pt) = []; %remove the source point. Not sure why the open brackets are used here. Can someone explain this?
end
chordx = ([xyNearest(:,:,1) - fpep(:,7)]);
chordy = ([xyNearest(:,:,2) - fpep(:,8)]);
chord = sqrt((chordx).^2 + (chordy).^2);
6 Comments
Rik
on 22 Oct 2019
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
As to your question: I don't think your code will become more clear if you rewrite this.
For the questions in your comments: the tilde supresses an output you're not going to use. So in this case your code only uses the second output of the sort function. The variable name gives a hint what that is, but if you want to know for sure you need to read the documentation.
Assigning [] to something is setting an empty array. That will remove the elements from an array. That is basic Matlab syntax that any crash course should teach you. However, since the variable is not used in the next loop iteration (where it is overwritten), this doesn't actually do something usefull.
Also, you should load to a struct. That way it is clear where your variables come from and would prevent the call to clearvars. (and because you're not opening any figures here, the close all isn't required either)
Steve
on 22 Oct 2019
Rik
on 22 Oct 2019
I have no clue what you are trying to do. Your explanation is a bit too short and general for me. The only thing I notice when running this code is that your first input to dot is 953x10x10, while the second is 953x10(x1). I'm not sure if you want to discard the third dimension, or if you wanted to have the same implicit expansion as you have in your for-loop (where you substract 1xn from nx1).
Anyway, below is my suggestion to slightly clean up your code. It still lacks a lot of comments, but that is up to you to write.
%load input matrix
S=load('fpep.mat');fpep=S.fpep;
n = 10;
%pre-allocate loop outputs
xyNearest= zeros( size(fpep,1) , n , 2);
adirector_x=zeros( size(fpep,1) , n , n);
adirector_y=zeros( size(fpep,1) , n , n);
bdirector_x=zeros( size(fpep,1) , n , n);
bdirector_y=zeros( size(fpep,1) , n , n);
cdirector_x=zeros( size(fpep,1) , n , n);
cdirector_y=zeros( size(fpep,1) , n , n);
for pt = 1 : size(fpep,1)
dist = sqrt((fpep(:,7)-fpep(pt,7)).^2 + (fpep(:,8)-fpep(pt,8)).^2);
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the source point
xyNearest(pt,:,:) = fpep(ascendIdx(1:n),7:8);
adirector_x(pt,:,:) = fpep(ascendIdx(1:n),1) - xyNearest(pt,:,1);
adirector_y(pt,:,:) = fpep(ascendIdx(1:n),2) - xyNearest(pt,:,2);
bdirector_x(pt,:,:) = fpep(ascendIdx(1:n),3) - xyNearest(pt,:,1);
bdirector_y(pt,:,:) = fpep(ascendIdx(1:n),4) - xyNearest(pt,:,2);
cdirector_x(pt,:,:) = fpep(ascendIdx(1:n),5) - xyNearest(pt,:,1);
cdirector_y(pt,:,:) = fpep(ascendIdx(1:n),6) - xyNearest(pt,:,2);
end
chordx = xyNearest(:,:,1) - fpep(:,7);
chordy = xyNearest(:,:,2) - fpep(:,8);
chord = sqrt((chordx).^2 + (chordy).^2);
adirector = sqrt((adirector_x).^2 + (adirector_y).^2);
bdirector = sqrt((bdirector_x).^2 + (bdirector_y).^2);
cdirector = sqrt((cdirector_x).^2 + (cdirector_y).^2);
CosTheta_a = dot(adirector,chord)/(norm(adirector)*norm(chord));%probably needs ./ and .* instead
CosTheta_b = dot(bdirector,chord)/(norm(bdirector)*norm(chord));%probably needs ./ and .* instead
CosTheta_c = dot(cdirector,chord)/(norm(cdirector)*norm(chord));%probably needs ./ and .* instead
Steve
on 22 Oct 2019
Rik
on 22 Oct 2019
How did you generate this plot? That might help in getting what you mean.
Steve
on 22 Oct 2019
Answers (0)
This question is closed.
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
