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
the cyclist 2019 年 8 月 7 日
編集済み: the cyclist 2019 年 8 月 7 日

0 投票

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
Tom 2019 年 8 月 7 日
@the cyclist: Yes that works, thanks a lot.
Tom
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
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.
Tom
Tom 2019 年 8 月 8 日
編集済み: Tom 2019 年 8 月 8 日
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.
the cyclist
the cyclist 2019 年 8 月 8 日
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
Tom 2019 年 8 月 8 日
Sorry, could you clarify where the formula goes when you say 'put your real formula here'?
the cyclist
the cyclist 2019 年 8 月 8 日
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
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.
the cyclist
the cyclist 2019 年 8 月 9 日
% 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 ExchangeProgramming についてさらに検索

タグ

質問済み:

Tom
2019 年 8 月 7 日

コメント済み:

2019 年 8 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by