# To write a For loop in which a variable is updated with each run.

2 views (last 30 days)
Andrei_43214 on 1 Oct 2022
Answered: Stephen23 on 1 Oct 2022
I want to write a script file that generates the Cartesian coordinates of the vertices of a regular Octadecagon (18-sided regular polygon) centered at the origin and with a radius, r = 1. The coordinates can be obtained by rotating repeatedly by the angle . This can be done by matrix multiplication of
by the coordinates of a vertex of the polygon as a column vector.The first vertex is the following:
Matrix multiplication ... until will give the coordinates of all 18 vertices. I've been trying to do this in a for loop which would ideally give me the 18 coordinates as an 18x2 array but I am still very new to coding and I'm not managing to do it. I have been trying to do this so far:
R = [cos(pi/9) -sin(pi/9); sin(pi/9) cos(pi/9)];
v_1 = [sin(pi/18); -cos(pi/18)];
A = [v_1'];
for r = 1:18
column = num2str(r);
v_column = R*v_column;
v_column+('1') = v_column';
A = [A;v_column+('1')];
end
I am unable to let Matlab know that I want to update v_column to the next integer with each run. Any help/tips would be greatly appreciated.
Basically, all I want is to make the following piece of code more efficient by using a for loop (if it can be done).
R = [cos(pi/9) -sin(pi/9); sin(pi/9) cos(pi/9)];
% For i = 1,...,18: v_i = (x_i,y_i) %
v_1 = [sin(pi/18); -cos(pi/18)];
v_2 = R*v_1;
v_3 = R*v_2;
v_4 = R*v_3;
v_5 = R*v_4;
v_6 = R*v_5;
v_7 = R*v_6;
v_8 = R*v_7;
v_9 = R*v_8;
v_10 = R*v_9;
v_11 = R*v_10;
v_12 = R*v_11;
v_13 = R*v_12;
v_14 = R*v_13;
v_15 = R*v_14;
v_16 = R*v_15;
v_17 = R*v_16;
v_18 = R*v_17;
A = [v_1';v_2';v_3';v_4';v_5';v_6';v_7';v_8';v_9';v_10';v_11';v_12';v_13';v_14';v_15';v_16';v_17';v_18']
##### 2 CommentsShowHide 1 older comment
Andrei_43214 on 1 Oct 2022
I don't want to plot the octadecagon; I just want Matlab to generate the 18 (x,y) coordinate points of the 18 vertices of the regular octadecagon as an 18x2 array where the first column denotes the x-coordinates and the second column denotes the y-coordinates, respectively. Consider the following the piece of code. I want to make this more efficient by using a for loop (if it can be done).
R = [cos(pi/9) -sin(pi/9); sin(pi/9) cos(pi/9)];
% For i = 1,...,18: v_i = (x_i,y_i) %
v_1 = [sin(pi/18); -cos(pi/18)];
v_2 = R*v_1;
v_3 = R*v_2;
v_4 = R*v_3;
v_5 = R*v_4;
v_6 = R*v_5;
v_7 = R*v_6;
v_8 = R*v_7;
v_9 = R*v_8;
v_10 = R*v_9;
v_11 = R*v_10;
v_12 = R*v_11;
v_13 = R*v_12;
v_14 = R*v_13;
v_15 = R*v_14;
v_16 = R*v_15;
v_17 = R*v_16;
v_18 = R*v_17;
A = [v_1';v_2';v_3';v_4';v_5';v_6';v_7';v_8';v_9';v_10';v_11';v_12';v_13';v_14';v_15';v_16';v_17';v_18']
A = 18×2
0.1736 -0.9848 0.5000 -0.8660 0.7660 -0.6428 0.9397 -0.3420 1.0000 -0.0000 0.9397 0.3420 0.7660 0.6428 0.5000 0.8660 0.1736 0.9848 -0.1736 0.9848

Stephen23 on 1 Oct 2022
"I want to make this more efficient by using a for loop (if it can be done)."
Of course it can be done, you just need to avoid numbering variable names like that.
Here is a basic MATLAB approach using one matrix and indexing:
R = [cos(pi/9) -sin(pi/9); sin(pi/9) cos(pi/9)];
A = [sin(pi/18), -cos(pi/18); nan(17,2)];
for k = 2:18
A(k,:) = R * A(k-1,:).';
end
display(A)
A = 18×2
0.1736 -0.9848 0.5000 -0.8660 0.7660 -0.6428 0.9397 -0.3420 1.0000 -0.0000 0.9397 0.3420 0.7660 0.6428 0.5000 0.8660 0.1736 0.9848 -0.1736 0.9848

### More Answers (1)

Matt J on 1 Oct 2022
Edited: Matt J on 1 Oct 2022
Why not just do,
pgon=nsidedpoly(18);
pgon.Vertices
ans = 18×2
-0.1736 -0.9848 -0.5000 -0.8660 -0.7660 -0.6428 -0.9397 -0.3420 -1.0000 0 -0.9397 0.3420 -0.7660 0.6428 -0.5000 0.8660 -0.1736 0.9848 0.1736 0.9848
plot(pgon); axis equal
##### 1 CommentShowHide None
Andrei_43214 on 1 Oct 2022
Hmm, that does indeed help. Now, the actual problem is to generate those coordinates for 5 different octadecagons (having radii varying by 1cm). I have 5 octadecagons, first having radius = 1.75, second having radius = 2.75, ... fifth having radius = 5.75. I want to generate those 18 coordinates for each octadecagon and then append them into one final array of size 90x2.
for r=1.75:1:5.75
pgon_r.Vertices
end
ans = 18×2
-0.3039 -1.7234 -0.8750 -1.5155 -1.3406 -1.1249 -1.6445 -0.5985 -1.7500 0 -1.6445 0.5985 -1.3406 1.1249 -0.8750 1.5155 -0.3039 1.7234 0.3039 1.7234
ans = 18×2
-0.4775 -2.7082 -1.3750 -2.3816 -2.1066 -1.7677 -2.5842 -0.9406 -2.7500 0 -2.5842 0.9406 -2.1066 1.7677 -1.3750 2.3816 -0.4775 2.7082 0.4775 2.7082
ans = 18×2
-0.6512 -3.6930 -1.8750 -3.2476 -2.8727 -2.4105 -3.5238 -1.2826 -3.7500 0 -3.5238 1.2826 -2.8727 2.4105 -1.8750 3.2476 -0.6512 3.6930 0.6512 3.6930
ans = 18×2
-0.8248 -4.6778 -2.3750 -4.1136 -3.6387 -3.0532 -4.4635 -1.6246 -4.7500 0 -4.4635 1.6246 -3.6387 3.0532 -2.3750 4.1136 -0.8248 4.6778 0.8248 4.6778
ans = 18×2
-0.9985 -5.6626 -2.8750 -4.9796 -4.4048 -3.6960 -5.4032 -1.9666 -5.7500 0 -5.4032 1.9666 -4.4048 3.6960 -2.8750 4.9796 -0.9985 5.6626 0.9985 5.6626

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by