# Creating the X and Y matrices with points of a plot

2 views (last 30 days)
Minas Emiris on 11 Apr 2018
Commented: dpb on 11 Apr 2018
I was wondering how to create two matrices with all the points on an plot, in the case where the values of y are found through a 'for' loop, implying that they are deleted every time the loop continues. For example, I am trying to create two matrices of X and Y summarising all the points of every single arc of angle pi/8 created by the following code:
figure;
theta = pi/8;
hold on
N = 200;
Rmax = 5;
R = linspace (0,Rmax,N);
for i = 1:N
x = linspace(R(i)*cos(theta),R(i),100);
y = sqrt (R(i)^2 - x.*x);
plot(x,y,'b');
end
I realise that if we have two matrices x1 and x2, we can combine them into a single one as X = [x1 ; x2], but I am not sure how to employ this having a 'for' loop. Also, if I manage this, I am afraid that the values of X will not be rearranged to be in ascending order within the matrix, and even if I manage this, then the values of a similar matrix Y (summarising all the values of Y and corresponding to the values of matrix X in appropriate order) will not be accordingly rearranged. Thus, if I attempt then to plot X versus Y, then the values will not give me the original shape.
dpb on 11 Apr 2018
1. Preallocate X, Y as N*100 and populate subsections in the loop
2. Use sortrows or sort with optional second output