user3743235
user3743235

Reputation: 159

Extract blocks of rows from a single-column dataset to separate sequential columns

I have a dataset that includes all data in 1 column. However a new sub-dataset occurs every n rows. Using MATLAB, I need to grab each of these sub-datasets and place in a separate sequential column. For example, this:

Column 1

1
3
2
1
3
2
1
3
2

into this:

Column 1 | Column 2 | Column 3 | ...

1  1  1
3  3  3
2  2  2

Upvotes: 2

Views: 119

Answers (1)

Engineero
Engineero

Reputation: 12928

You can use the reshape command like so:

my_matrix = reshape(my_vector, num_rows, num_cols);

You can also replace num_cols in the above with [] to have MATLAB automatically figure out how many columns are needed to fit the data into num_rows rows. Similarly, you can replace num_rows with [] and have MATLAB figure out the number of rows needed to fit the data into num_cols columns.

Note that MATLAB will throw an error if my_vector does not contain exactly num_rows * num_cols elements. In other words, it will not pad with zeros or truncate your data if the sizes do not match.

Upvotes: 2

Related Questions