Using rdivide for multidimensional matrices
2 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
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.
0 个评论
更多回答(2 个)
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), []);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!