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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues