MATLAB Answers

Tom
0

How to Index Points and Store them all in an Array

Asked by Tom
on 26 Oct 2018
Latest activity Commented on by Tom
on 10 Nov 2018
I am working with a surface on which I place an arbitrary number of nodes. The points are indexed in spherical polar coordinates via a formula for each coordinate (plus 2 points at the poles). Is there a simple way that I can set out the formulae with MATLAB, choose the index number I would like to go up to and then have the coordinates for each node placed one by one into the columns of a matrix (so the first row would be the theta coordinates for the position vectors of all the nodes, and so on with the phi coordinates for the nodes on the second row and the radial coordinates on the third row).

  2 Comments

That would depend upon the available formula, whether index is a parameter to it. If only the coordinates are input the formula then it would depend upon whether there is a good way to calculate the coordinates from the index number, or an effective way to generate enough of the coordinates to determine the index order.
I can post the formula here as it is taken from a paper which I will reference here if necessary:
The first two points are located at the poles of a unit sphere and the other points are then distributed around the sphere at coordinates according to these formula:
\theta = i*\Pi/(N_\theta + 1)
\phi = 2*\Pi(j-1)/N_\phi
r = 1
where i runs from 1 to N_\theta and j runs from 1 to N_\phi, such that the total number of points is N = (N_\theta * N_\phi) +2. I can follow the formula by hand up to a small number of points on the sphere, but I feel like there must be a way to specify the maximum values of N_theta and N_phi such that the points are generated over the sphere and an array is created for their positions, as it would obviously be impossible to find the values of 100 points by hand, say.

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 27 Oct 2018
 Accepted Answer

[i, j] = ndgrid(1:N_theta, 1:N_phi);
theta = i .* pi ./ (N_theta + 1);
phi = 2 .* pi .* (j-1) ./ N_phi;
r = 1;
coords = [theta(:), phi(:)];
coords(:,3) = r;

  3 Comments

This doesn't seem to work, I will post what I am intending:
[i, j] = ndgrid(1:N_theta, 1:N_phi);
theta = i .* pi ./ (N_theta + 1);
phi = 2 .* pi .* (j-1) ./ N_phi;
r = 1;
b = [theta(:,1), phi(:,2),r(:,3)];
So if I define N_theta and N_phi to be both be 2 for example, I need 4 position vectors such that the alphas of each vector can form the first row, the phi values of each vector form the second row of a matrix and the constant radial value forms the third row, this code above does not do that although I have tried to modify it (the two extra position vectors for the poles could then be added in after this matrix has been created if necessary).
[i, j] = ndgrid(1:N_theta, 1:N_phi);
theta = i .* pi ./ (N_theta + 1);
phi = 2 .* pi .* (j-1) ./ N_phi;
r = 1;
b = [theta(:).'; phi(:).'; r * ones(1,numel(theta))];
This seems to have worked but I also need to add the vectors for the nodes at the north and south poles, so I need to add the code in which adds two vectors to the array for [0 0 r] and [0 0 -r]. I tried something like
b(1:3,i+1)=[0 0 r];
b(1:3,i+2)=[0 0 -r];
but it didn't seem to work.

Sign in to comment.