Clear Filters
Clear Filters

How to multiply a vector 3 times to get a 3 dimentional matrix

1 view (last 30 days)
Hi,
Suppose I have a vector B = [1 2 3]. Then
B*B' = [1*1 1*2 1*3 ; 2*1 2*2 2*3 ; 3*1 3*2 3*3]
Now, I need to multiply the new matrix B*B' again with B, in order to get the following 3 dimentional matrix C:
C(:,:,1) = [1*1*1 1*2*1 1*3*1 ; 2*1*1 2*2*1 2*3*1 ; 3*1*1 3*2*1 3*3*1]
C(:,:,2) = [1*1*2 1*2*2 1*3*2 ; 2*1*2 2*2*2 2*3*2 ; 3*1*2 3*2*2 3*3*2]
C(:,:,3) = [1*1*3 1*2*3 1*3*3 ; 2*1*3 2*2*3 2*3*3 ; 3*1*3 3*2*3 3*3*3]
Any ideas how can I do that?
(My original vector is long so I can not do this manually..)
Thanks!

Accepted Answer

Adam
Adam on 28 Nov 2016
Edited: Adam on 28 Nov 2016
B * B' .* reshape( B, [1 1 3] );
if B starts as a column matrix. In your case above B * B' would in itself result in a scalar as B is a row matrix.
Obviously if you start with a row vector you can just do
B' * B .* reshape( B, [1 1 3] )

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!