3d arrays Matrix multiplication with a vector
5 views (last 30 days)
Show older comments
i have 3 D matrix <64x64x64 double> i want to mutiply it with a vector <64x1 double>.
0 Comments
Answers (2)
BhaTTa
on 12 Jun 2024
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 Comments
Steven Lord
on 12 Jun 2024
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 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!