How can I convert into new coordinates over a loop?

6 visualizaciones (últimos 30 días)
Tom
Tom el 25 de Oct. de 2018
Comentada: Star Strider el 10 de Nov. de 2018
I am working with a set of vectors for a calculation: the vectors are obtained in spherical polar coordinates and I then need to convert them all so that they are in Cartesian coordinates.
Basically, I have a matrix where I store the positions of a set of N vectors in spherical polar coordinates. So the first row is composed of the theta component of each of the vectors, the second row is the phi component, and the third is the radial coordinate and I would then like to form a new matrix which is the same but now the first row is the x component, the second row is the y component, and the third row is the z component. However, when I try to create this new matrix, MATLAB just says that the dimensionality of the matrices doesn't work, for example:
r(1,1:10)= sin(alpha)*cos(phi);
where alpha and phi have been specified as the row vectors formed from the first and second rows of the original matrix.
The example above is if I were to try to create the first row of the new matrix for 10 vectors in the original spherical polar coordinates. I have looked it up and the advice is that I should basically make MATLAB to do it by looping over the combinations which are needed but I am unsure how to do this, could someone advise?

Respuesta aceptada

Star Strider
Star Strider el 25 de Oct. de 2018
This task might be easier with the sph2cart (link) function.
  4 comentarios
Tom
Tom el 29 de Oct. de 2018
Editada: Tom el 29 de Oct. de 2018
Hang on, I think your answer would work in theory but it assumes that you have already created the array explicitly using that construction. In my case I just follow a formula and it spits out some numerical values into an array which are in spherical coordinates ie. I just happen to know they are in spherical coordinates but they are only values. I just need some loop which converts each of the entries using the usual formulae and makes a new matrix where the entries are now in Cartesian coordinates.
Star Strider
Star Strider el 29 de Oct. de 2018
If your vectors are in the format that sph2cart expects, you should be able to use it without modification. The arguments can be ‘a scalar, vector, matrix, or multidimensional array.’
I do not understand the problem you are having with it.

Iniciar sesión para comentar.

Más respuestas (1)

Tom
Tom el 10 de Nov. de 2018
Editada: madhan ravi el 10 de Nov. de 2018
I will show you what I am intending, basically I have a piece of code which creates a 3 x N array depending on the values I choose for N_alpha and N_phi, let's say it's a 3 x 4 array, so I have four position vectors with the first row corresponding to the theta component of each vector, the second row is the phi component of the vector and the third row is for the radial component of the vector.
[i, j] = ndgrid(1:N_alpha, 1:N_phi);
alpha = (i-1)*2*pi/N_alpha;
phi = (j)*pi/(N_phi+1);
r = 1;
b = [alpha(:).'; phi(:).'; r * ones(1,numel(alpha))];
I would then like a new array B which is the same but with everything converted to Cartesian coordinates, if I try
B=sph2cart(alpha,phi,r);
it creates a 2 x 2 array which is smaller than the matrix I started with. If I do sph2cart(b) it says that the dimensions don't agree.
  7 comentarios
Tom
Tom el 10 de Nov. de 2018
The output seems to be smaller, so if I feed in a 3x4 vector I get 2x2 back, so something must be going wrong. Basically I have a point which has a column vector in spherical coordinates on a sphere, then I take several such vectors for different points and slot them next to each other to make a matrix. I just want to take the position vectors in that matrix and change them so they are in Cartesian coordinates, there must be a simple way of doing this as I cannot get the function to work.
Star Strider
Star Strider el 10 de Nov. de 2018
‘Basically I have a point which has a column vector in spherical coordinates on a sphere, then I take several such vectors for different points and slot them next to each other to make a matrix.’
Apparently, I still do not understand the problem you are having.
If your data vectors are gridded (have regularly-repeating patterns in the x- and y-coordinates), you can make them into a matrix using the reshape function first, then sph2cart.
If you simply have vectors in spherical coordinates that you want to change to Cartesian coordinates, you can do that easily enough with sph2cart.
You can then plot them using any function you wish. I use scatter3 in my example. Any other 3-D plotting function will work.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by