I didn't understand how put the inputs in tprod function

Francesco asked on 19 Aug 2011
Latest activity: Edit by Francesco on 20 Aug 2011

How have I to put the input in tprod to have the same result of the code that follows?

a=rand(3,4,5);
b=rand(4,6);
for i=1:5
       c(:,i,:)=a(:,:,i)*b;
end

[edit: matrix dimension corrected]

0 comments

Tags

Products

    2 answers

    Fangjun Jiang answered on 19 Aug 2011
    %%
    a=rand(3,4,5);
    b=rand(3,4);
    c=zeros(size(a));
    
    for i=1:5
           c(:,:,i)=a(:,:,i).*b;
    end
    

    Or do you mean this link? There is a 3-D example there. http://www.mathworks.com/matlabcentral/fileexchange/16275-tprod-arbitary-tensor-products-between-n-d-arrays/content/tprod.m

    Okay, I tried, it should be like below. The original tprod.m does contain incorrect examples.

    %% Your code
    a=rand(3,4,5);
    b=rand(4,6);
    for i=1:5
           c(:,i,:)=a(:,:,i)*b;
    end
    
    % use tprod
    Z=tprod(a,[1 -1 2],b,[-1 3]);
    
    err=abs(Z-c);
    err=sum(err(:))
    
    err =
    
           0

    3 comments

    Francesco on 19 Aug 2011

    Thank you, Fangjun Jiang, but I had in mind
    c=tprod(a,[ ... something ... ],b,[ ... something ... ]);

    Fangjun Jiang on 20 Aug 2011

    That is the problem! You need to at least provide some explanation about tprod. It's not a MATLAB function anywhere. Do you mean this?
    http://www.mathworks.com/matlabcentral/fileexchange/16275

    Francesco on 20 Aug 2011

    Yes, I meant right it.

    Walter Roberson answered on 19 Aug 2011

    What is "tprod" ? It does not seem to be indexed on the MATHWORKS web site.

    Your code produces an error, as at each step you are attempting to do a matrix multiplication of a 3 x 4 matrix, by a 3 x 4 matrix. Either you need to use .* to do element-by-element multiplication, or you need to change your formula.

    1 comment

    Francesco on 20 Aug 2011

    tprod is a function to multiply n-dimensional arrays (http://www.mathworks.com/matlabcentral/fileexchange/16275-tprod-arbitary-tensor-products-between-n-d-arrays)
    About the dimensions: you're right, I corrected them right now, thanks.

    Contact us at files@mathworks.com