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
@the cyclist: Yes that works, thanks a lot.
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.
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
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.
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
