How can I convert into new coordinates over a loop?
古いコメントを表示
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?
採用された回答
その他の回答 (1 件)
Tom
2018 年 11 月 10 日
編集済み: madhan ravi
2018 年 11 月 10 日
7 件のコメント
Star Strider
2018 年 11 月 10 日
What are representative values for ‘N_alpha’ and ‘N_phi’?
Tom
2018 年 11 月 10 日
Star Strider
2018 年 11 月 10 日
I am still not certain what you want.
Try this:
b = [alpha(:).'; phi(:).'; r * ones(1,numel(alpha))];
[x,y,z]=sph2cart(b(1,:),b(2,:),b(3,:));
figure
plot3(x, y, z)
grid on
% axis equal
Star Strider
2018 年 11 月 10 日
The output is the same size as the input, in this example 3 (1x4) vectors. If you want longer output vectors, you need to provide longer input vectors.
Example —
[theta,phi] = ndgrid(0:0.15:2*pi);
r = 2*ones(size(theta));
[x,y,z,] = sph2cart(theta,phi,r);
figure
scatter3(x(:), y(:), z(:), '.')
grid on
axis equal
Tom
2018 年 11 月 10 日
Star Strider
2018 年 11 月 10 日
‘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.
カテゴリ
ヘルプ センター および File Exchange で Time Series Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!