Using num2cell function in Matlab

Posted by Braimaster on Sat, 18 Sep 2021 02:36:13 +0200

catalogue

grammar

explain

Example

Convert array to cell array

Creates a cell array of numeric arrays

Merge multiple dimensions

The num2cell function converts an array into a cell array of the same size.

grammar

C = num2cell(A)
C = num2cell(A,dim)

  1. Input, specified as any type of multidimensional array.
  2. The dimension of A, specified as A positive integer or A positive integer vector. dim   Must be between 1 and   ndims(A). The elements do not need to be arranged in numerical order. however   num2cell   The array dimension of each cell in C is replaced to match the order of the specified dimensions.
  3. The generated array is returned as A cell array. C   The size of depends on the size of A and the value of dim.
  • If not specified   dim, then   C   Size and   A   Same.

  • If   dim   Is a scalar, then   C   contain   numel(A)/size(A,dim)   Cell. If   dim   If it is 1 or 2, each cell contains a column vector or row vector respectively. If   dim  > 2, then each cell contains a second cell   dim   The length of the dimension is   size(A,dim)   And the other dimensions are single dimensions.

    For example, given a 4 × seven × 3 array   A. This window displays   num2cell   How to create and   dim   value   1,2   and   three   Corresponding cell.

  • If   dim   Is included   N   A vector of values, then   C   have   numel(A)/prod([size(A,dim(1)),...,size(A,vdim(N))])   Cells. Each cell contains a second cell   dim(i)   The length of the dimension is   size(A,dim(i))   And other dimensions are arrays of single dimensions.

    For example, given a 4 × seven × 3 array, you can   dim   Specify as a positive integer vector to create cell arrays of different dimensions.

explain

    C =   num2cell(A)   By will   A   Each element of is placed in   C   In a separate cell of the array   A   Convert to cell array   C. num2cell   The function converts an array with any data type (even a non numeric type).

    C =   num2cell(A,dim)   take   A   The content of is divided into   C   A separate cell in which   dim   Specifies that each cell contains   A   Which dimension of the. dim   It can be a scalar or vector of dimensions. For example, if   A   If there are 2 rows and 3 columns, then:

  • num2cell(A,1)   Create a 1 × 3-cell array   C. Each cell contains   A   Of 2 × Column 1.

  • num2cell(A,2)   Create a 2 × 1-cell array   C. Each cell contains   A   Of 1 × Three lines.

  • num2cell(A,[1 2])   Create a 1 × 1-cell array   C. Each cell contains   A   The entire array.

Example

Convert array to cell array

         Put all the elements of a numeric array into separate cells.

a = magic(3)
a = 3×3

     8     1     6
     3     5     7
     4     9     2

         Using the function:

c = num2cell(a)
c=3×3 cell array
    {[8]}    {[1]}    {[6]}
    {[3]}    {[5]}    {[7]}
    {[4]}    {[9]}    {[2]}

         Put the letters of a word into the cells of the array.

a = ['four';'five';'nine']
a = 3x4 char array
    'four'
    'five'
    'nine'

c = num2cell(a)
c = 3x4 cell
    {'f'}    {'o'}    {'u'}    {'r'}
    {'f'}    {'i'}    {'v'}    {'e'}
    {'n'}    {'i'}    {'n'}    {'e'}

Creates a cell array of numeric arrays

         Generate a 4 × three × 2, and then create an array containing 4 × 1 column vector × three × Cell array of 2.

A = reshape(1:12,4,3);
A(:,:,2) = A*10
A = 
A(:,:,1) =

     1     5     9
     2     6    10
     3     7    11
     4     8    12


A(:,:,2) =

    10    50    90
    20    60   100
    30    70   110
    40    80   120

C = num2cell(A,1)
C = 1x3x2 cell array
C(:,:,1) = 

    {4x1 double}    {4x1 double}    {4x1 double}


C(:,:,2) = 

    {4x1 double}    {4x1 double}    {4x1 double}

         Each 4 × 1 vector contains along   A   Elements of the first dimension of:

C{1}
ans = 4×1

     1
     2
     3
     4

         Create 1 × 3 numerical array 4 × one × 2-cell array.

C = num2cell(A,2)
C = 4x1x2 cell array
C(:,:,1) = 

    {1x3 double}
    {1x3 double}
    {1x3 double}
    {1x3 double}


C(:,:,2) = 

    {1x3 double}
    {1x3 double}
    {1x3 double}
    {1x3 double}

         Each 1 × The 3-line vector contains along   A   Elements of the second dimension:

C{1}
ans = 1×3

     1     5     9

         Finally, create 1 × one × 2 of numeric array 4 × 3-cell array.

C = num2cell(A,3)
C=4×3 cell array
    {1x1x2 double}    {1x1x2 double}    {1x1x2 double}
    {1x1x2 double}    {1x1x2 double}    {1x1x2 double}
    {1x1x2 double}    {1x1x2 double}    {1x1x2 double}
    {1x1x2 double}    {1x1x2 double}    {1x1x2 double}

         Each 1 × one × 2 vector contains along   A   Elements of the third dimension:

C{1}
ans = 
ans(:,:,1) =

     1


ans(:,:,2) =

    10

Merge multiple dimensions

         Create a cell array by merging numerical arrays of various dimensions.

A = reshape(1:12,4,3);
A(:,:,2) = A*10
A = 
A(:,:,1) =

     1     5     9
     2     6    10
     3     7    11
     4     8    12


A(:,:,2) =

    10    50    90
    20    60   100
    30    70   110
    40    80   120

c = num2cell(A,[1 3])
c=1×3 cell array
    {4x1x2 double}    {4x1x2 double}    {4x1x2 double}

         Each 4 × one × 2 the array contains edges   A   Elements of the first and third dimensions of:

c{1}
ans = 
ans(:,:,1) =

     1
     2
     3
     4


ans(:,:,2) =

    10
    20
    30
    40

c = num2cell(A,[2 3])
c=4×1 cell array
    {1x3x2 double}
    {1x3x2 double}
    {1x3x2 double}
    {1x3x2 double}

Topics: MATLAB