3D Matrix multiplication with vector

前端 未结 4 522
一生所求
一生所求 2020-12-10 05:22

This bothers me a bit:

Suppose you have a matrix with three layers.

Is there a simple way to multiply this matrix with a vector of three elements so that the

4条回答
  •  我在风中等你
    2020-12-10 06:06

    In addition to gnovice's answer, you can also replicate your vector along the other dimensions and do a direct element wise multiplication.

    A=randn(1000,1000,3);%# this is your matrix
    vector=[1,2,3];%# this is your vector
    
    [dim1 dim2 ~]=size(A);
    replicatedVector=repmat(reshape(vector,1,1,3),[dim1,dim2,1]);
    out=A.*replicatedVector;
    

提交回复
热议问题