Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transpose a vector into the third dimension

Tags:

matlab

Given a vector v = [1 2 3 4 5 6] how do I turn it into:

v = 
    v(:,:,1) =  1
    v(:,:,2) =  2
    v(:,:,3) =  3
    v(:,:,4) =  4
    v(:,:,5) =  5
    v(:,:,6) =  6

i.e., transpose between the second (row) and third dimension?

like image 394
James Avatar asked May 03 '26 20:05

James


2 Answers

The answer is to use the permute command:

permute([1 2 3 4 5 6], [3 1 2])

It accepts non-existing dimensions: the second argument specifies swapping the first existing dimension with the third existing dimension (none), which creates a 'singleton' first dimension in the result.

like image 58
James Avatar answered May 05 '26 12:05

James


You can also reshape:

reshape([1 2 3 4 5],1,1,[])

EDIT (in response to comment): actually, it does:


>> reshape([1 2 3 4 5],1,1,[])

ans(:,:,1) =

     1


ans(:,:,2) =

     2


ans(:,:,3) =

     3


ans(:,:,4) =

     4


ans(:,:,5) =

     5
like image 31
lsfinn Avatar answered May 05 '26 12:05

lsfinn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!