How to multiply Multidimensional Arrays with a column vector
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to multiply A with B so that C(:,:,1) is equal to A(:,:,1)*B(1) and C(:,:,2) is equal to A(:,:,2)*B(2)
>> A = cat(3, [2 8; 0 5], [1 3; 7 9])
A(:,:,1) =
2 8
0 5
A(:,:,2) =
1 3
7 9
>> B=[1 2]'
B =
1
2
I'm looking to get this:
>> C=???
C(:,:,1) =
2 8
0 5
C(:,:,2) =
2 6
14 18
0 commentaires
Réponses (3)
sixwwwwww
le 28 Oct 2013
Modifié(e) : sixwwwwww
le 28 Oct 2013
Dear Tristan, here is the code which performs the task:
A = cat(3, [2 8; 0 5], [1 3; 7 9]);
B=[1 2]';
for i = 1:length(B)
C(:, :, i) = B(i) * A(:, :, i);
end
disp(C)
I hope it helps. Good luck!
5 commentaires
Shambhavi Singh
le 14 Fév 2019
This should work too
C=A.*permute(B,[3 2 1]),3);
Permute switches the rows in B (So the elements of a column vector) with the third dimension
James Tursa
le 28 Oct 2013
C = mtimesx(A,reshape(B,1,1,[]));
You can find MTIMESX here:
0 commentaires
Pavel Chmelar
le 12 Jan 2016
Clear Matlab solution according sixwwwwww and Tristan:
B=zeros(1,1,2);
B(:)=[1,2];
C=bsxfun(@times,A,B);
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!