- Preallocate the required output arrays.
- Use indexing in the nested loops.

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

2 views (last 30 days)

Show older comments

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

### Answers (2)

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

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

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!