3d arrays Matrix multiplication with a vector
5 次查看(过去 30 天)
显示 更早的评论
i have 3 D matrix <64x64x64 double> i want to mutiply it with a vector <64x1 double>.
0 个评论
回答(2 个)
BhaTTa
2024-6-12
To multiply a 3D matrix by a vector in MATLAB, you need to decide how you want this multiplication to behave. Since direct multiplication like this isn't standard in linear algebra, you have a few options depending on what you're trying to achieve.Element-wise multiplication for each slice
If you want to multiply each slice (a 64x64 matrix) of your 3D matrix by the vector (64x1) element-wise, where each element of the vector multiplies the corresponding row of each 64x64 slice, you can use the following approach:
% Assuming A is your 3D matrix (64x64x64) and v is your vector (64x1)
result = zeros(size(A)); % Initialize the result array with the same size as A
for i = 1:size(A,3) % Loop through each slice
for j = 1:length(v) % Loop through each element of the vector
result(:,j,i) = A(:,j,i) * v(j); % Multiply each row of the slice by the vector element
end
end
0 个评论
Steven Lord
2024-6-12
This wasn't an option when the question was asked originally, but since release R2020b you can use pagemtimes. Let's make a small sample data set.
A = randi(5, [4 4 3]);
b = [1; 2; 3; 4];
C = pagemtimes(A, b)
To check the answer we can multiply one of the pages of A by b and check it against the corresponding page in C.
pageOfA = A(:, :, 2)*b
pageOfC = C(:, :, 2)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!