简体   繁体   中英

Converting Rows into Columns in a cell array in MATLAB

I have a 99x1 cell array and I would like to convert it into a 33x3 cell array for example.

I would like the first 3 rows of the 99x1 cell array to make up the first row in the 33x3 cell array and then the 3rd through 6th row in the 99x1 cell array to make up the second row in the 33x3 cell array.

I also need the data when being reshaped to go over column by column before it goes down. For example I would need:

1 2 3 4

to become

1, 2; 3, 4

not

1, 3; 2, 4

Help with this would be greatly appreciated

You can simply use the reshape -function. Since reshape(yourcell,[],3) would first fill the first column and then the second and so on instead of row-wise, you will need to combine it with the transpose operator .' :

newcell=reshape(yourcell,3,[]).'

This way, you will first create a 3x33 cell using the reshape and then transform it into the desired 33x3 cell. The [] tells reshape to create as many columns as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM