Creating an Array for Different Radial Positions
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am basically looking to take a point in spherical polar coordinates such that theta = 0, phi = pi/2 and r = 1, then extend outwards at the same angles but increasing the radius by 0.2 each time until a radius of 10 is reached.
Corresponding to each of these points on a radial line, I then need a 3 x 46 array such that each 3 x 1 vector in the array corresponds to the position of each of the consecutive radial points in Cartesian coordinates, what would be the easiest way of doing this? Please let me know if this is not clear.
採用された回答
the cyclist
2019 年 8 月 7 日
編集済み: the cyclist
2019 年 8 月 7 日
theta = 0;
phi = pi/2;
r = 1 : 0.2 : 10;
theta = repmat(theta,size(r));
phi = repmat(phi, size(r));
[x,y,z] = sph2cart(theta,phi,r);
xyz = [x; y; z];
9 件のコメント
Tom
2019 年 8 月 7 日
@the cyclist: Yes that works, thanks a lot.
Tom
2019 年 8 月 8 日
I don't know if this makes sense, but now that I have this 3 x 46 array for the positions, let's say the first vector in the array is p_1, the next one where you move out along the radius by 0.2 is p_2.
I also have a 3 x 100 array, which I am viewing as the position vectors of 100 points (always with radius less than 1). I basically need to create a matrix which looks as follows, where each distance is the row vector obtained by vector subtraction of the two vectors:
[Distance from p_1 to first vector in 3 x 100 array Distance from p_1 to second vector etc ]
[Distance from p_2 to first vector in 3 x 100 array Distance from p_2 to second vector etc ]
[ Distance from p_3 ..... ]
and so on, until all the positions in the 3 x 46 array are covered, let me know if my intention is not clear.
the cyclist
2019 年 8 月 8 日
I think it mostly makes sense.
Is the distance for any given pair a 3x1 array? Or you want to sum somehow over that vector?
What size/shape should the final output be? You are going to have 3 * 46 * 100 values.
Yes, the distance is a 3 x 1 array, so it's like the radial vector which you get by going from the position vector p_1 to a vector in the 3 x 100 array via vector addition (or a subtraction, in this case).
In fact, the distance vector rij needs to go into a function, but it is the same function each time, just the distance which is plugged in changes.
J=1/(8*pi*0.7*sqrt(2/pi))*(eye(3)/norm(rij)+(rij'*rij)/((norm(rij))^3));
However, I have just noticed that the function is going to produce a 3 x 3 matrix when you plug in one of the distances, so the final size is going to be 300 x 138, rather than 300 x 46.
There is no requirement on the shape, and it does not need to be a square matrix. Please let me know if this isn't clear what I mean.
Any objection to making a 4-D array?
theta = 0;
phi = pi/2;
r = 1 : 0.2 : 10;
theta = repmat(theta,size(r));
phi = repmat(phi, size(r));
[x,y,z] = sph2cart(theta,phi,r);
xyz = [x; y; z];
% Create other matrix at random
otherMatrix = rand(3,100);
% Initialize distance output array
distanceOutput = nan(46,100,3,3);
for ii = 1:46
for jj = 1:100
distanceOutput(ii,jj,:,:) = xyz(:,ii)*otherMatrix(:,jj)'; % Put your real formula here
end
end
Tom
2019 年 8 月 8 日
Sorry, could you clarify where the formula goes when you say 'put your real formula here'?
I just put in an arbitrary calculation that would yield a 3x3 array:
xyz(:,ii)*otherMatrix(:,jj)'
but you should put in your actual distance calculation that gives the correct 3x3 array. It might be the subtraction
xyz(:,ii) - otherMatrix(:,jj)'
but I was not sure (and did not carefully look at your calculation of J). I just wanted to show you how to make the structure.
Tom
2019 年 8 月 9 日
Is there any way of doing it and avoiding a 4D array, I know it might be more efficient, but I basically want the 300 x 138 array, which I can then multiply with a 300 x 1 vector which I have prepared to get my solution to the problem.
% Initialize distance output array
distanceOutput = nan(300,138);
for ii = 1:46
for jj = 1:100
distanceOutput(3*jj-2:3*jj,3*ii-2:3*ii) = xyz(:,ii) - otherMatrix(:,jj)'; % Put your real formula here
end
end
To be clear, what this code is going to do is the following:
For the i'th (of 46) vector in xyz, and the j'th (of 100) vector in otherMatrix, there is an operation that leads to a 3x3 matrix. That 3x3 matrix is going to be placed into the array distanceOutput (like a tile in a rectangular floor), positioned i steps down and j steps to the right.
I hope that makes sense, and is what you want.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
