3d arrays Matrix multiplication with a vector
조회 수: 12 (최근 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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!