bsxfun implementation in matrix multiplication

前端 未结 5 567
孤城傲影
孤城傲影 2020-11-28 14:36

As always trying to learn more from you, I was hoping I could receive some help with the following code.

I need to accomplish the following:

1) I have a vect

5条回答
  •  清歌不尽
    2020-11-28 15:06

    If your vector x is of lenght = 12 and your matrix of size 3x4, I don't think that using one or the other would change much in term of time. If you are working with higher size matrix and vector, now that might become an issue.

    So first of all, we want to multiply a vector with a matrix. In the for-loop method, that would give something like that :

    s = size(A);
    new_matrix(s(1),s(2),numel(x)) = zeros;   %This is for pre-allocating. If you have a big vector or matrix, this will help a lot time efficiently.
    
    for i = 1:numel(x)
        new_matrix(:,:,i)= A.*x(i)
    end
    

    This will give you 3D matrix, with each 3rd dimension being a result of your multiplication. If this is not what you are looking for, I'll be adding another solution which might be more time efficient with bigger matrixes and vectors.

提交回复
热议问题