MATLAB Answers

What is the best way to set up a nested for loop?

3 views (last 30 days)
Steve
Steve on 22 Sep 2019
Commented: Steve on 24 Sep 2019
Hi,
I need help creating an efficient, nested for-loop. I have to run through a couple of equations on a few of variables but need to know the best way to increment these variables and to store their new values.
Thanks,
Steve

  5 Comments

Show 2 older comments
Steve
Steve on 22 Sep 2019
How would this be applied to my specific files? (see attached).
darova
darova on 22 Sep 2019
Your code looks ok. What do you need help?
Steve
Steve on 22 Sep 2019
i need to create another loop to find and store the 3 angles that each Triplet makes with their corresponding F_point. Then, I would like to store all of this in one file.

Sign in to comment.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 22 Sep 2019
Hi Steven,
You loop is not doing much. SInce you are saving the last value from your nested loops. If this is what you want then, just remove the loops as follows:
load('Triplets');
load('F_points');
i=953; j=953;
xC = Triplets{i}(2,1);
xB = Triplets{i}(2,2);
xA = Triplets{i}(2,3);
yC = Triplets{i}(1,1);
yB = Triplets{i}(1,2);
yA = Triplets{i}(1,3);
yb = F_points{j}(2,1);
xb = F_points{j}(1,1);
xyb = [yb,xb];
xye = [Triplets{j}(1,3), Triplets{j}(2,3); Triplets{j}(1,2) Triplets{j}(2,2); Triplets{j}(1,1) Triplets{j}(2,1)];
CosTheta1 = dot(xye(1,:)-xyb(1,:),xye(2,:)-xyb(1,:))/(norm(xye(1,:)-xyb(1,:))*norm(xye(2,:)-xyb(1,:)));
ThetaInDegrees1 = acosd(CosTheta1);
CosTheta2 = dot(xye(2,:)-xyb(1,:),xye(3,:)-xyb(1,:))/(norm(xye(2,:)-xyb(1,:))*norm(xye(3,:)-xyb(1,:)));
ThetaInDegrees2 = acosd(CosTheta2);
CosTheta3 = dot(xye(3,:)-xyb(1,:),xye(1,:)-xyb(1,:))/(norm(xye(3,:)-xyb(1,:))*norm(xye(1,:)-xyb(1,:)));
ThetaInDegrees3 = acosd(CosTheta3);
degsum=ThetaInDegrees1+ThetaInDegrees2+ThetaInDegrees3;
If you do wnat to save all values of variables (xC, xB, ... xye, ... degsum) from your computations, then you'd need to specify the indexes and then you'd need to do the memory allocation:
xA = zeros(1,953); % Memory allocation
xB = zeros(1,953);
xC = zeros(1,953);
yA = zeros(1,953);
yB = zeros(1,953);
yC = zeros(1,953);
...
for i = 1 : length (Triplets)
for j = 1 : i
xC(i) = Triplets{i}(2,1);
xB(i) = Triplets{i}(2,2);
xA(i) = Triplets{i}(2,3);
yC(i) = Triplets{i}(1,1);
yB(i) = Triplets{i}(1,2);
yA(i) = Triplets{i}(1,3);
yb(j) = F_points{j}(2,1);
xb(j) = F_points{j}(1,1);
...
end
end

  2 Comments

Steve
Steve on 22 Sep 2019
Thanks for the info. What about the angle finding portion of the code? See below:
CosTheta1 = dot(xye(1,:)-xyb(1,:),xye(2,:)-xyb(1,:))/(norm(xye(1,:)-xyb(1,:))*norm(xye(2,:)-xyb(1,:)));
ThetaInDegrees1 = acosd(CosTheta1);
CosTheta2 = dot(xye(2,:)-xyb(1,:),xye(3,:)-xyb(1,:))/(norm(xye(2,:)-xyb(1,:))*norm(xye(3,:)-xyb(1,:)));
ThetaInDegrees2 = acosd(CosTheta2);
CosTheta3 = dot(xye(3,:)-xyb(1,:),xye(1,:)-xyb(1,:))/(norm(xye(3,:)-xyb(1,:))*norm(xye(1,:)-xyb(1,:)));
ThetaInDegrees3 = acosd(CosTheta3);
degsum=ThetaInDegrees1+ThetaInDegrees2+ThetaInDegrees3;

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 23 Sep 2019
Hi Steven,
Here is the complete solution of your problem:
clearvars
%% Part 0. Loading data
load('Triplets');
load('F_points');
N = length(Triplets);
%% Part 1. Saving all values of arrays from the calculations
% Memory allocation
xA = zeros(1,N);
xB = zeros(1,N);
xC = zeros(1,N);
yA = zeros(1,N);
yB = zeros(1,N);
yC = zeros(1,N);
xb = zeros(1,N);
yb = zeros(1,N);
for ii = 1 : N
for jj = 1 : N
xC(ii) = Triplets{ii}(2,1);
xB(ii) = Triplets{ii}(2,2);
xA(ii) = Triplets{ii}(2,3);
yC(ii) = Triplets{ii}(1,1);
yB(ii) = Triplets{ii}(1,2);
yA(ii) = Triplets{ii}(1,3);
yb(jj) = F_points{jj}(2,1);
xb(jj) = F_points{jj}(1,1);
end
end
xyb = [yb; xb]'; % Augmented matrix array = xyb
% Memory allocation:
xye1 = zeros(2,N);
xye2 = zeros(2,N);
xye3 = zeros(2,N);
for k = 1:N
xye1(:,k) = [Triplets{k}(1,3), Triplets{k}(2,3)];
xye2(:,k) = [Triplets{k}(1,2) Triplets{k}(2,2)];
xye3(:,k) = [Triplets{k}(1,1) Triplets{k}(2,1)];
end
XYE0 = [xye1', xye2', xye3']; % Augmented matrix array = 'xye'
%% Part 2. Compute Angles
% Memory allocation:
CosTheta1 = zeros(1,N);
ThetaInDegrees1=zeros(1,N);
CosTheta2 = zeros(1,N);
ThetaInDegrees2 = zeros(1,N);
CosTheta3 = zeros(1,N);
ThetaInDegrees3 = zeros(1,N);
for jj = 1:N
CosTheta1(jj) = dot(XYE0(jj,1:2)-xyb(jj, :),XYE0(jj,3:4)-xyb(jj,:))/(norm(XYE0(jj,1:2)-xyb(jj,:))*norm(XYE0(jj,3:4)-xyb(jj,:)));
ThetaInDegrees1(jj) = acosd(CosTheta1(jj));
CosTheta2(jj) = dot(XYE0(jj,3:4)-xyb(jj, :),XYE0(jj,5:6)-xyb(jj,:))/(norm(XYE0(jj,3:4)-xyb(jj,:))*norm(XYE0(jj,5:6)-xyb(jj,:)));
ThetaInDegrees2(jj) = acosd(CosTheta2(jj));
CosTheta3(jj) = dot(XYE0(jj,5:6)-xyb(jj, :),XYE0(jj,1:2)-xyb(jj,:))/(norm(XYE0(jj,5:6)-xyb(jj,:))*norm(XYE0(jj,1:2)-xyb(jj,:)));
ThetaInDegrees3(jj) = acosd(CosTheta3(jj));
degsum(jj)=ThetaInDegrees1(jj)+ThetaInDegrees2(jj)+ThetaInDegrees3(jj);
end
good luck

  4 Comments

Show 1 older comment
Steve
Steve on 24 Sep 2019
I just wanted to add that I loaded different F_point and Triplets files and the code ran, but the angles are not correct. They are all coming up as decimals (instead of values like 120 for example). Any idea why this might be the case?
Thanks!
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 Sep 2019
Ok. Glad that can of some help. Hit accept of this answer, pl.
Make sure that your formualtions are giving the values in degrees not radians. If other way around, then you should use conversion the values rad2deg(). With rad2deg() you will get the values in degrees. Always watch out the size match.
Steve
Steve on 24 Sep 2019
I don't think the angles are in radians or degrees. My original code portion for finding the angles gave true angles in degrees. Attached are the new input files; they may not be in the correct row/column arrangements, and that may be causing the problem with the angles. How does it look to you?
Thanks!

Sign in to comment.

Sign in to answer this question.