简体   繁体   English

在 MATLAB 中将单元格数组中的行转换为列

[英]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.我有一个 99x1 单元阵列,例如,我想将其转换为 33x3 单元阵列。

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.我希望 99x1 单元阵列的前 3 行组成 33x3 单元阵列中的第一行,然后 99x1 单元阵列中的第 3 到第 6 行组成 33x3 单元阵列中的第二行。

I also need the data when being reshaped to go over column by column before it goes down.在数据下降之前,我还需要逐列重塑为 go 的数据。 For example I would need:例如我需要:

1 2 3 4 1 2 3 4

to become成为

1, 2; 1、2; 3, 4 3、4

not不是

1, 3; 1、3; 2, 4 2、4

Help with this would be greatly appreciated对此的帮助将不胜感激

You can simply use the reshape -function.您可以简单地使用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 .'由于reshape(yourcell,[],3)将首先填充第一列,然后是第二列,依此类推,而不是逐行填充,因此您需要将其与转置运算符结合使用.' :

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.这样,您将首先使用reshape创建一个3x33单元格,然后将其转换为所需的33x3单元格。 The [] tells reshape to create as many columns as needed. []告诉 reshape 根据需要创建尽可能多的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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