Asked by Tom
on 26 Oct 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).

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;

Tom
on 29 Oct 2018

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).

Walter Roberson
on 29 Oct 2018

[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))];

Tom
on 10 Nov 2018

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.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/426363-how-to-index-points-and-store-them-all-in-an-array#comment_628741

## Tom (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/426363-how-to-index-points-and-store-them-all-in-an-array#comment_628982

Sign in to comment.