How to multiply a matrix by certain numbers?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to multiply a matrix by a column vector in this way:
(4 2 3 8;7 9 1 5;6 4 8 3) * (4;8;2)
and then the result i want to get is:
(4*4 2*4 3*4 8*4;7*8 9*8 1*8 5*8;6*2 4*2 8*2 3*2)
3 commentaires
madhan ravi
le 30 Juin 2019
xy = repmat(x,1,1,numel(x)) .* reshape(y,1,1,[]); % for version <=2016b bsxfun(@times,repmat(x,1,1,numel(x)) , reshape(y,1,1,[]))
Wanted = reshape(squeeze(xy).',1,[])
Réponse acceptée
Stalin Samuel
le 23 Sep 2015
A = [4 2 3 8;7 9 1 5;6 4 8 3];
B = [4;8;2]
for i = 1:length(B)
C(i,:) = A(i,:)*B(i)
end
1 commentaire
Stephen23
le 23 Sep 2015
Modifié(e) : Stephen23
le 30 Juin 2019
Note that:
- The array C expands on every iteration, which is slow and inefficient. Preallcoation would resolve this.
- Using a loop is more complex than using vectorized code.
- The variable name i should be avoided, as it is the name of the imaginary unit.
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!