How to Index Points and Store them all in an Array

21 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2018-10-27
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.
Tom
Tom 2018-10-27
编辑:Walter Roberson 2018-10-27
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-10-27
[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 个评论
Walter Roberson
Walter Roberson 2018-10-29
[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
Tom 2018-11-10
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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by