Maddy
Maddy

Reputation: 2570

Change a column_vector to a matrix in MATLAB

I have a column vector that needs to be changed into a matrix. The size of matrix is specified and can change. Please suggest a vectorized solution.

rows = 3 ; cols = 4 ; %matrix elements for this case = 12

colvector = [ 2;4;5;8;10;14;16;18;20;21;28;30] ;

desired_mat = [ ...
               2     4     5     8
              10    14    16    18
              20    21    28    30 ] ;

Thanks!

Upvotes: 0

Views: 57

Answers (1)

Adam
Adam

Reputation: 17329

The reshape function does that:

>> colvector = [ 2;4;5;8;10;14;16;18;20;21;28;30] ;
>> A = reshape(colvector, 3, 4)

A =

     2     8    16    21
     4    10    18    28
     5    14    20    30

Upvotes: 1

Related Questions