3d arrays Matrix multiplication with a vector

12 ビュー (過去 30 日間)
nihed sboui
nihed sboui 2020 年 2 月 28 日
回答済み: Steven Lord 2024 年 6 月 12 日
i have 3 D matrix <64x64x64 double> i want to mutiply it with a vector <64x1 double>.

回答 (2 件)

BhaTTa
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

Steven Lord
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)
C =
C(:,:,1) = 22 24 50 44 C(:,:,2) = 30 39 29 30 C(:,:,3) = 28 26 30 37
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
pageOfA = 4x1
30 39 29 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
pageOfC = C(:, :, 2)
pageOfC = 4x1
30 39 29 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by