How to change a vector to create a matrix?

Here is what I have:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 

And here is what I want to get:

 [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] 

The number of rows and columns (3 and 4 in the example) is already known.

How can I do it?

+4
source share
2 answers

This guide says

mat = vec2mat (vec, matcol) converts the vec vector into a matrix with matcol columns, creating one row at a time. If the length of vec is not a multiple of matcol, then additional zeros are placed in the last row of the mat. Matrix mat has ceil strings (length (vec) / matcol).

+2
source

reshape

b = reshape(a, 4, 3)' will work for your example. Elements are taken from the original and inserted into the new matrix in columns.

In addition, reshape is a built-in MATLAB function. There are other solutions, such as vec2mat , that require a communication toolbar.

+5
source

Source: https://habr.com/ru/post/1400761/


All Articles