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

조회 수: 1 (최근 30일)
Steve
Steve 2019년 9월 22일
댓글: Steve 2019년 9월 24일
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
darova
darova 2019년 9월 22일
Your code looks ok. What do you need help?
Steve
Steve 2019년 9월 22일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 9월 22일
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
Steve
Steve 2019년 9월 22일
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;

댓글을 달려면 로그인하십시오.


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 9월 23일
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
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 9월 24일
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 2019년 9월 24일
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!

댓글을 달려면 로그인하십시오.

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by