Matlab - Mnożenie macierzy przez każdą macierz macierzy 3d
Mam dwa pytania matlab, które wydają się ściśle powiązane.
Chcę znaleźć najbardziej efektywny sposób (bez pętli?), Aby pomnożyć macierz (A x A) z każdą pojedynczą macierzą macierzy 3d (A x A x N). Chciałbym także pobrać ślad każdego z tych produktów.http://en.wikipedia.org/wiki/Matrix_multiplication#Frobenius_product
To jest wewnętrzny frobenius produkt. Na poniższym kodzie, który mam poniżej, używam jego drugorzędnej definicji, która jest bardziej wydajna.
Chcę pomnożyć każdy element wektora (N x 1) za pomocą „odpowiedniej” macierzy macierzy 3d (A x A x N).
function Y_returned = problem_1(X_matrix, weight_matrix)
% X_matrix is the randn(50, 50, 2000) matrix
% weight_matrix is the randn(50, 50) matrix
[~, ~, number_of_matries] = size(X_matrix);
Y_returned = zeros(number_of_matries, 1);
for i = 1:number_of_matries
% Y_returned(i) = trace(X_matrix(:,:,i) * weight_matrix');
temp1 = X_matrix(:,:,i)';
temp2 = weight_matrix';
Y_returned(i) = temp1(:)' * temp2(:);
end
end
function output = problem_2(vector, matrix)
% matrix is the randn(50, 50, 2000) matrix
% vector is the randn(2000, 1) vector
[n1, n2, number_of_matries] = size(matrix);
output = zeros(n1, n2, number_of_matries);
for i = 1:number_of_matries
output(:, :, i) = vector(i) .* matrix(:, :, i);
end
output = sum(output, 3);
end