Convert an Array from Spherical to Cartesian Coordinates in MATLAB
up vote
0
down vote
favorite
I am working in MATLAB with a formula which indexes points on a unit sphere in spherical coordinates.
[i, j] = ndgrid(1:N_theta, 1:N_phi);
theta = (i-1)*2*pi/N_theta;
phi = (j)*pi/(N_phi+1);
r = 1;
b = [theta(:).'; phi(:).'; r * ones(1,numel(theta))];
Let's assume I choose particular values for N_theta
and N_phi
and that each point has a position vector in spherical coordinates, where the first component is theta
, the second component is phi
and the third component is r
. Running the formula then creates an array (I've called it b
) which takes the position vector for each of the N points and slots them all next to each other to make a 3xN matrix.
I essentially just need to take that array and convert it so it's the same array with the vectors all next to each other but now the position vectors are in Cartesian coordinates (we could call the new array B
).
I have looked up the sph2cart
function in MATLAB which is designed for that purpose but I'm not sure if I am using it correctly and am hoping someone could point it what I am doing wrong. I have tried this, for example
B=sph2cart(b(1,:),b(2,:),b(3,:));
and
B = sph2cart(theta,phi,r);
but they both create matrices which are too small, so something is obviously going wrong.
matlab cartesian-coordinates spherical-coordinate
add a comment |
up vote
0
down vote
favorite
I am working in MATLAB with a formula which indexes points on a unit sphere in spherical coordinates.
[i, j] = ndgrid(1:N_theta, 1:N_phi);
theta = (i-1)*2*pi/N_theta;
phi = (j)*pi/(N_phi+1);
r = 1;
b = [theta(:).'; phi(:).'; r * ones(1,numel(theta))];
Let's assume I choose particular values for N_theta
and N_phi
and that each point has a position vector in spherical coordinates, where the first component is theta
, the second component is phi
and the third component is r
. Running the formula then creates an array (I've called it b
) which takes the position vector for each of the N points and slots them all next to each other to make a 3xN matrix.
I essentially just need to take that array and convert it so it's the same array with the vectors all next to each other but now the position vectors are in Cartesian coordinates (we could call the new array B
).
I have looked up the sph2cart
function in MATLAB which is designed for that purpose but I'm not sure if I am using it correctly and am hoping someone could point it what I am doing wrong. I have tried this, for example
B=sph2cart(b(1,:),b(2,:),b(3,:));
and
B = sph2cart(theta,phi,r);
but they both create matrices which are too small, so something is obviously going wrong.
matlab cartesian-coordinates spherical-coordinate
What is the size of the matrices you get when running sph2cart? Have you double checked the size of your inputs?
– Justin Wager
Nov 11 at 2:28
For future visitors, Cartesian to Spherical at stackoverflow.com/questions/21214098/…
– SecretAgentMan
Nov 11 at 5:06
Yes, so for example, if I have 3x4 matrix as input the output I get when I try to do spherical to cartesian is 2 x 2 even though it should be the same size.
– Tom
Nov 11 at 12:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am working in MATLAB with a formula which indexes points on a unit sphere in spherical coordinates.
[i, j] = ndgrid(1:N_theta, 1:N_phi);
theta = (i-1)*2*pi/N_theta;
phi = (j)*pi/(N_phi+1);
r = 1;
b = [theta(:).'; phi(:).'; r * ones(1,numel(theta))];
Let's assume I choose particular values for N_theta
and N_phi
and that each point has a position vector in spherical coordinates, where the first component is theta
, the second component is phi
and the third component is r
. Running the formula then creates an array (I've called it b
) which takes the position vector for each of the N points and slots them all next to each other to make a 3xN matrix.
I essentially just need to take that array and convert it so it's the same array with the vectors all next to each other but now the position vectors are in Cartesian coordinates (we could call the new array B
).
I have looked up the sph2cart
function in MATLAB which is designed for that purpose but I'm not sure if I am using it correctly and am hoping someone could point it what I am doing wrong. I have tried this, for example
B=sph2cart(b(1,:),b(2,:),b(3,:));
and
B = sph2cart(theta,phi,r);
but they both create matrices which are too small, so something is obviously going wrong.
matlab cartesian-coordinates spherical-coordinate
I am working in MATLAB with a formula which indexes points on a unit sphere in spherical coordinates.
[i, j] = ndgrid(1:N_theta, 1:N_phi);
theta = (i-1)*2*pi/N_theta;
phi = (j)*pi/(N_phi+1);
r = 1;
b = [theta(:).'; phi(:).'; r * ones(1,numel(theta))];
Let's assume I choose particular values for N_theta
and N_phi
and that each point has a position vector in spherical coordinates, where the first component is theta
, the second component is phi
and the third component is r
. Running the formula then creates an array (I've called it b
) which takes the position vector for each of the N points and slots them all next to each other to make a 3xN matrix.
I essentially just need to take that array and convert it so it's the same array with the vectors all next to each other but now the position vectors are in Cartesian coordinates (we could call the new array B
).
I have looked up the sph2cart
function in MATLAB which is designed for that purpose but I'm not sure if I am using it correctly and am hoping someone could point it what I am doing wrong. I have tried this, for example
B=sph2cart(b(1,:),b(2,:),b(3,:));
and
B = sph2cart(theta,phi,r);
but they both create matrices which are too small, so something is obviously going wrong.
matlab cartesian-coordinates spherical-coordinate
matlab cartesian-coordinates spherical-coordinate
edited Nov 11 at 11:22
SecretAgentMan
405110
405110
asked Nov 10 at 22:27
Tom
1016
1016
What is the size of the matrices you get when running sph2cart? Have you double checked the size of your inputs?
– Justin Wager
Nov 11 at 2:28
For future visitors, Cartesian to Spherical at stackoverflow.com/questions/21214098/…
– SecretAgentMan
Nov 11 at 5:06
Yes, so for example, if I have 3x4 matrix as input the output I get when I try to do spherical to cartesian is 2 x 2 even though it should be the same size.
– Tom
Nov 11 at 12:00
add a comment |
What is the size of the matrices you get when running sph2cart? Have you double checked the size of your inputs?
– Justin Wager
Nov 11 at 2:28
For future visitors, Cartesian to Spherical at stackoverflow.com/questions/21214098/…
– SecretAgentMan
Nov 11 at 5:06
Yes, so for example, if I have 3x4 matrix as input the output I get when I try to do spherical to cartesian is 2 x 2 even though it should be the same size.
– Tom
Nov 11 at 12:00
What is the size of the matrices you get when running sph2cart? Have you double checked the size of your inputs?
– Justin Wager
Nov 11 at 2:28
What is the size of the matrices you get when running sph2cart? Have you double checked the size of your inputs?
– Justin Wager
Nov 11 at 2:28
For future visitors, Cartesian to Spherical at stackoverflow.com/questions/21214098/…
– SecretAgentMan
Nov 11 at 5:06
For future visitors, Cartesian to Spherical at stackoverflow.com/questions/21214098/…
– SecretAgentMan
Nov 11 at 5:06
Yes, so for example, if I have 3x4 matrix as input the output I get when I try to do spherical to cartesian is 2 x 2 even though it should be the same size.
– Tom
Nov 11 at 12:00
Yes, so for example, if I have 3x4 matrix as input the output I get when I try to do spherical to cartesian is 2 x 2 even though it should be the same size.
– Tom
Nov 11 at 12:00
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244047%2fconvert-an-array-from-spherical-to-cartesian-coordinates-in-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the size of the matrices you get when running sph2cart? Have you double checked the size of your inputs?
– Justin Wager
Nov 11 at 2:28
For future visitors, Cartesian to Spherical at stackoverflow.com/questions/21214098/…
– SecretAgentMan
Nov 11 at 5:06
Yes, so for example, if I have 3x4 matrix as input the output I get when I try to do spherical to cartesian is 2 x 2 even though it should be the same size.
– Tom
Nov 11 at 12:00