How to Index Points and Store them all in an Array

3 vues (au cours des 30 derniers jours)
Tom
Tom le 26 Oct 2018
Commenté : Tom le 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 commentaires
Walter Roberson
Walter Roberson le 27 Oct 2018
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 le 27 Oct 2018
Modifié(e) : Walter Roberson le 27 Oct 2018
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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 27 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;
coords = [theta(:), phi(:)];
coords(:,3) = r;
  3 commentaires
Walter Roberson
Walter Roberson le 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
Tom le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by