使用 MAT2CELL 的 MATLAB
我有以下矩阵
letter=[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ...
a b c d e f g h ii jj k l m o p q r s t u v w x y z];
number=[one two three four five six seven eight nine zero];
character =[number letter];
字符变成 42 x 1464 的矩阵,但我想使用 mat2cell 将其分解为 42 x 24 的单元格。 我该怎么办,每次我尝试都会出错???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑到您在文中描述的问题,您给出的示例矩阵没有多大意义。如果您有一个大小为 42×1464 的矩阵,并且您想要使用 MAT2CELL 将其分解为包含 42×24 元素的元胞数组,您可以执行以下操作:
MAT2CELL 定义如何沿行维度在单元格之间分解行。在本例中,单个值 42(即
nRows
)表示生成的元胞数组将具有一行,并且元胞将包含 42 行的矩阵。MAT2CELL 的第三个输入定义了如何将列分解为沿列维度的单元格。在本例中,它是一个 61 个元素(即
nCols/nSubCols
)的行向量,其元素均包含数字 24(即nSubCols
)。这表明生成的元胞数组将有 61 列,并且元胞将包含 24 列的矩阵。最终结果是
cellArray
最终成为一个 1×61 元胞数组,其中每个元胞包含一个mat
的 42×24 子矩阵。当
nCols
是nSubCols
的精确倍数时,上述方法有效。如果不是,那么您将必须以异构方式将列分配到单元格(即每个单元格的子矩阵中可能有不同数量的列)。 此讨论了处理这种情况的一些方法其他SO问题。The example matrices you give don't make much sense given the problem you describe in the text. If you have a matrix of size 42-by-1464, and you want to use MAT2CELL to break it up into a cell array containing elements that are 42-by-24, you can do the following:
The second input to MAT2CELL defines how the rows will be broken up among cells along the row dimension. In this case, a single value of 42 (i.e.
nRows
) indicates that the resulting cell array will have one row and the cells will contain matrices with 42 rows.The third input to MAT2CELL defines how the columns will be broken up among cells along the column dimension. In this case, it is a 61 element (i.e.
nCols/nSubCols
) row vector whose elements all contain the number 24 (i.e.nSubCols
). This indicates that the resulting cell array will have 61 columns and the cells will contain matrices with 24 columns.The net result is that
cellArray
ends up being a 1-by-61 cell array where each cell contains a 42-by-24 submatrix ofmat
.The above works when
nCols
is an exact multiple ofnSubCols
. If it isn't, then you will have to distribute columns to your cells in a heterogeneous manner (i.e. each cell may have a different number of columns in its submatrix). Some ways to deal with such a situation are discussed in this other SO question.