Using rdivide for multidimensional matrices

1 Ansicht (letzte 30 Tage)
deathtime
deathtime am 12 Sep. 2023
Bearbeitet: Bruno Luong am 13 Sep. 2023
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;

Akzeptierte Antwort

Matt J
Matt J am 12 Sep. 2023
Bearbeitet: Matt J am 12 Sep. 2023
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.

Weitere Antworten (2)

Bruno Luong
Bruno Luong am 12 Sep. 2023
Bearbeitet: Bruno Luong am 13 Sep. 2023
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 am 12 Sep. 2023
Bearbeitet: Matt J am 12 Sep. 2023
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;

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by