Switching an array of Vectors from Cartesian to Spherical Coordinates

12 ビュー (過去 30 日間)
Tom
Tom 2019 年 8 月 25 日
編集済み: dpb 2019 年 8 月 25 日
I have a 3 x 46 array called V which I am viewing as an array of 46 column vectors in Cartesian components, I am attempting to switch the components to spherical, so that I have an array of 46 column vectors in spherical polar coordinates r, theta, phi using the cart2sph function but I cannot seem to get the syntax right?
Do I need to separate out the rows first
X=V(1,:);
Y=V(2,:);
Z=V(3,:);
or can the function be applied directly to the array to make a conversion?

回答 (3 件)

dpb
dpb 2019 年 8 月 25 日
編集済み: dpb 2019 年 8 月 25 日
The documentation syntax is pretty clear--
"[azimuth,elevation,r] = cart2sph(x,y,z) transforms corresponding elements of the Cartesian coordinate arrays x, y, and z to spherical coordinates azimuth, elevation, and r."
Yes, you must supply three separate x,y,z coordinate arrays to cart2sph because it can operate on either scalar, vector or array inputs and so the three coordinates have to be separated.
If you have the array, you don't need to create explicit temporaries, just pass the references...
[az,el,r]=cart2sph(V(1,:),V(2,:),V(3,:));
  4 件のコメント
dpb
dpb 2019 年 8 月 25 日
Well, it is what it is... :)
Altho, yes, it could have been different. But, there hasn't seemed to be any overall coherent interface design regimen established by TMW to try to create such consistency, unfortunately, and with the plethora of new toolboxes and data classes and the transitioning to named parameter/object-oriented interfaces from the classic procedural ones, it's only getting worse with time.
dpb
dpb 2019 年 8 月 25 日
編集済み: dpb 2019 年 8 月 25 日
"I already tried cart2sph(X,Y,Z) and I got a 1x46 array when I am looking for a 3x46 array..."
Because you didn't provide anything except the default ans location for Matlab to return the three variables [az,el,r] so all you got back was az; el and r weren't returned because that syntax didn't ask for them.

サインインしてコメントする。


David Hill
David Hill 2019 年 8 月 25 日
編集済み: David Hill 2019 年 8 月 25 日
[ax,el,r]=cart2sph(V(:,1),V(:,2),V(:,3));
V=[ax,el,r];
I believe the above should work for you.
  4 件のコメント
Bruno Luong
Bruno Luong 2019 年 8 月 25 日
編集済み: Bruno Luong 2019 年 8 月 25 日
Tom has xyz data ranged in 3-rows not 3-columns
David Hill
David Hill 2019 年 8 月 25 日
Sorry, just change V to V'. I thought it was a 46x3 array.

サインインしてコメントする。


Bruno Luong
Bruno Luong 2019 年 8 月 25 日
編集済み: Bruno Luong 2019 年 8 月 25 日
OK since nobody care to give you the complete commands I'll
[az,el,r] = cart2sph(V(1,:),V(2,:),V(3,:));
S = [az; el; r]; % <- here is your 3 x 46 array of spherical coordinates
  4 件のコメント
Bruno Luong
Bruno Luong 2019 年 8 月 25 日
Waoh; you coudn't figure out where is the radial component from
S = [az; el; r]
It's a third row if you pay attention.
dpb
dpb 2019 年 8 月 25 日
編集済み: dpb 2019 年 8 月 25 日
To repeat my first comment--
The documentation syntax is pretty clear--
"[azimuth,elevation,r] = cart2sph(x,y,z) transforms corresponding elements of the Cartesian coordinate arrays x, y, and z to spherical coordinates azimuth, elevation, and r."
Nothing says you can't build an output array in whatever form/order you want it, but as Bruno says, "pay attention" to the documentation--it 'splains it all explicitly and even gives examples.
What might be "easier" is all dependent upon what you are actually going to do with the result which we only have in some general use of the word plot but no specific type nor code to know anything about the specific syntax needed to do so.
The above syntax directly from cart2sph has already built such a set of row vectors; if you want to use them. There's no need to necessarily build an array again at all excepting that is the form you asked for in the original question so folks were simply trying to oblige.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by