使用MAT2CELL的MATLAB [英] MATLAB using MAT2CELL

查看:170
本文介绍了使用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];

字符到1464年变成42的矩阵,但是我想使用mat2cell将其分解为42到24的单元. 每次尝试出现错误时,我该怎么做?

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????

推荐答案

鉴于您在课文中描述的问题,给出的示例矩阵没有多大意义.如果您的矩阵大小为42×1464,并且要使用 MAT2CELL 将其分解为包含42×24元素的单元格数组,您可以执行以下操作:

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

MAT2CELL 的第二个输入定义行的显示方式沿行维度在单元格之间分解.在这种情况下,单个值为42(即nRows)表示生成的单元格数组将只有一行,而这些单元格将包含具有42行的矩阵.

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.

MAT2CELL 的第三个输入定义了列的显示方式沿列维度在单元格之间分解.在这种情况下,它是一个61个元素(即nCols/nSubCols)行向量,其元素都包含数字24(即nSubCols).这表明生成的单元格数组将具有61列,而单元格将包含24列的矩阵.

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.

最终结果是,cellArray最终是1×61单元格阵列,其中每个单元格包含mat的42×24子矩阵.

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.

nColsnSubCols的精确倍数时,以上方法适用.如果不是,那么您将不得不以不同的方式将列分配到您的单元中(即,每个单元在其子矩阵中可能具有不同数量的列). 此方法中讨论了一些解决这种情况的方法其他问题.

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.

这篇关于使用MAT2CELL的MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆