Using rdivide for multidimensional matrices

조회 수: 2 (최근 30일)
deathtime
deathtime 2023년 9월 12일
편집: Bruno Luong 2023년 9월 13일
I have a 5 by 10 by 3 force matrix:
F = rand(5, 10, 3);
The first dimension represents grid points on a mesh (5 mesh points).
The second dimension represents different conditions (10 different conditions).
The third dimension represents 3D components of force (3 components: x, y and z respectively).
I also have a basis vector matrix which I plan to use for a transformation:
basisVectorMat = [0.9659 -0.2588 0; 0 0 -1; 0.2588 0.9659 0];
I would like to transform all the x, y and z force components of F for 3 mesh points (1st, 3rd and 5th) points for 3 different conditions (the 2nd, 4th and 7th elements of the condition dimension). This is a reverse transformation, so the operation to carry out would be F_transformed = F * inv(basisVectorMat), or as I have been advised by Matlab help: F_transformed = F / basisVectorMat.
In order to make it clear, if I just had a single array representing the force [FX, FY, FZ] (so just a single mesh point and condition), this is how I would carry out the operation:
F = rand(1, 3);
F_transformed = F / basisVectorMat
How would I do the operation for the initial matrix defined with multiple mesh points and conditions, for the aforementioned indices, in a vectorized fashion.
My guess would have been to do something like the following:
F = rand(5, 10, 3);
F_transformed = F;
F_transformed([1 3 4], [2 4 7], :) = F([1 3 4], [2 4 7], :) / basisVectorMat;

채택된 답변

Matt J
Matt J 2023년 9월 12일
편집: Matt J 2023년 9월 12일
One way:
Fp=permute( F([1,3,5],[2,4,7],:), [1,3,2]);
Fp_transformed= pagerdivide( Fp , basisVectorMat);
F_transformed([1 3 4], [2 4 7], :) =ipermute(Fp_transformed, [1,3,2]);
Of course, it would be better had you not chosen to have the x,y,z components spread along the 3rd dimension. Then there would be no need to permute, which is expensive.

추가 답변 (2개)

Bruno Luong
Bruno Luong 2023년 9월 12일
편집: Bruno Luong 2023년 9월 13일
Put the first extraction of two subindices as a multi-row matrix, do the algebra calculation then put it back.
F = rand(5, 10, 3);
pntidx = [1 3 4];
condidx = [2 4 7];
F_transformed = F;
X = reshape(F_transformed(pntidx,condidx,:),[], size(F,3));
Y = X/basisVectorMat;
F_transformed(pntidx,condidx,:) = reshape(Y, length(pntidx),length(condidx), []);

Matt J
Matt J 2023년 9월 12일
편집: Matt J 2023년 9월 12일
Using the KronProd class, downloadable from here,
you could do,
B=KronProd({1,basisVectorMat},[1,1,2],[3,3,nan]);
F_transformed([1 3 4], [2 4 7], :) = F([1 3 4], [2 4 7], :) / B;

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by