MATLAB using MAT2CELL
i have the following matrices
开发者_JAVA百科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????
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.
精彩评论