使用 MAT2CELL 的 MATLAB

发布于 2024-10-11 13:40:45 字数 336 浏览 1 评论 0 原文

我有以下矩阵

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 的单元格。 我该怎么办,每次我尝试都会出错???

i have the following matrices

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];

Character becomes a matrix of 42 by 1464, but i would like to break it into cells of 42 by 24 using mat2cell.
How can i please do it, every time i try i get an error????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送舟行 2024-10-18 13:40:45

考虑到您在文中描述的问题,您给出的示例矩阵没有多大意义。如果您有一个大小为 42×1464 的矩阵,并且您想要使用 MAT2CELL 将其分解为包含 42×24 元素的元胞数组,您可以执行以下操作:

mat = repmat('a',42,1464);  %# A sample character matrix containing all 'a's
[nRows nCols] = size(mat);  %# Get the number of rows and columns in mat
nSubCols = 24;              %# The number of desired columns in each submatrix
cellArray = mat2cell(mat,nRows,nSubCols.*ones(1,nCols/nSubCols));

MAT2CELL 定义如何沿行维度在单元格之间分解行。在本例中,单个值 42(即 nRows)表示生成的元胞数组将具有一行,并且元胞将包含 42 行的矩阵。

MAT2CELL 的第三个输入定义了如何将列分解为沿列维度的单元格。在本例中,它是一个 61 个元素(即 nCols/nSubCols)的行向量,其元素均包含数字 24(即 nSubCols)。这表明生成的元胞数组将有 61 列,并且元胞将包含 24 列的矩阵。

最终结果是 cellArray 最终成为一个 1×61 元胞数组,其中每个元胞包含一个 mat 的 42×24 子矩阵。

nColsnSubCols 的精确倍数时,上述方法有效。如果不是,那么您将必须以异构方式将列分配到单元格(即每个单元格的子矩阵中可能有不同数量的列)。 此讨论了处理这种情况的一些方法其他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:

mat = repmat('a',42,1464);  %# A sample character matrix containing all 'a's
[nRows nCols] = size(mat);  %# Get the number of rows and columns in mat
nSubCols = 24;              %# The number of desired columns in each submatrix
cellArray = mat2cell(mat,nRows,nSubCols.*ones(1,nCols/nSubCols));

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 of mat.

The above works when nCols is an exact multiple of nSubCols. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文