Matlab cell Array Usage

Source: Internet
Author: User

Array of cells:

The cellular array is a special data type of MATLAB, which can be regarded as an all-encompassing universal matrix, or generalized matrix. The elements that make up a cell array can be constants or constants of any data type, each element can have a different size and memory footprint, and the contents of each element can be completely different, so the elements of a cell array are called cells. As with the normal numerical matrix, the memory space of the cell array is also allocated dynamically.

(1) Creation of an array of cells



>> a={' matlab ', 20;ones (2,3), 1:10}

A =

' Matlab ' [20]
[2x3 Double] [1x10 Double]

>> b=[{' matlab '},{20};{ Ones (2,3)},{1:10}]

b =

' Matlab ' [20]
[2x3 Double] [1x10 Double]

>> c={10}

c =

[10]

>> C (={2})

c =

[10] [2]

>> C (2,2) ={5}

c =

[10] [2]
[] [5]

>> IsEqual (A, B)

Ans =

1

>> whos
Name Size Bytes Class Attributes

A 2x2 388 cell
Ans 1x1 1 logical
b 2x2 388 Cell
C 2x2 208 Cell

Create an array of cells using the cell function, which is an empty cell. The primary purpose of the cell function to create an empty cell array is to pre-allocate contiguous storage space for the array, saving memory footprint and improving execution efficiency.

>> A=cell (1)

A =

{[]}

>> (B=cell)

b =

[]     []

>> C=cell (3,3)

c =

[]     []     []
[]     []     []
[]     []     []

>> D=cell (2,2,2)

D (:,:, 1) =

[]     []
[]     []

D (:,:, 2) =

[]     []
[]     []

>> whos
Name Size Bytes Class Attributes

A 1x1 4 cell
Ans 1x1 1 logical
B 1x2 8 Cell
C 3x3 Cell
D 2x2x2 Cell

(2) data acquisition of a cellular array

Reading data from a cellular array can be saved as a standard array or a new array of cells, or an array is taken out for calculation. Access to data in a cell array can be done by the subscript of the cell content, with the cell array name increasing the parentheses {}. Values in curly braces denote the subscript of a cell. such as a{1,2} represents the cell in the second column of the first row in the cell array.

>> a={20, ' matlab '; ones (2,3), 1:3}

A =

[+] ' matlab '
[2x3 Double] [1x3 Double]

>> (Str=a)

str =

' Matlab '

>> Class (str)

Ans =

Cell

>> str=a{1,2}

str =

Matlab

>> Class (str)

Ans =

Char

() and {} are essentially different, curly braces are used to represent the contents of the cell, and the parentheses represent the specified cell.

A =

[+] ' matlab '
[2x3 Double] [1x3 Double]

>> a{2,1} (2,2)

Ans =

0.9134

>> a{2,1} (2,3)

Ans =

0.0975

>> a{1,2} (2)

Ans =

A

Using the cell's subscript, you can assign a subset of a cell array to another variable, creating a new array of cells.

>> a=[{1},{2},{3};{ 4},{5},{6}; {7},{8},{9}]

A =

[1] [2] [3]
[4] [5] [6]
[7] [8] [9]

>> B=a (2:3,2:3)

b =

[5] [6]
[8] [9]

>> C=a (1:3,2:3)

c =

[2] [3]
[5] [6]
[8] [9]

This example uses a cell subscript to create a new cell array B and C, which shows that B and C are part of the cell array A.

(3) Deletion and remodeling of cellular arrays

To delete a row or column in a cell array, you can use a colon to represent the row or column in the cell array, and then assign an empty matrix to it.



a={20, ' matlab '; ones (2,3), 1:3}

A =

[+] ' matlab '
[2x3 Double] [1x3 Double]

>> A (1,:) =[]

A =

[2x3 Double] [1x3 Double]

>> a={20, ' matlab '; ones (2,3), 1:3};
>> a{1}=[]

A =

[] ' matlab '
[2x3 Double] [1x3 Double]

>> A (1) =[]

A =

[2x3 Double] ' Matlab ' [1x3 double]

>> A (2) =[]

A =

[2x3 Double] [1x3 Double]

>> A (=[)]
??? A null assignment can has only one Non-colon index.

>> A (1) =[]

A =

[1x3 Double]

The array of ingots, like other arrays, can also be changed by the reshape function, changing the cell array to the same number of elements as the original cell array, and cannot add or remove elements from the cell array by altering the shape.



>> A=cell (bis)

A =

[]     []     []     []
[]     []     []     []
[]     []     []     []
[]     []     []     []

>> size (a)

Ans =

4 4

>> B=reshape (a,2,8)

b =

[]     []     []     []     []     []    []     []
[]     []     []     []     []     []    []     []

>> size (b)

Ans =

2 8



(5) Operation function in a cell array

Cell: Creating an empty array of cells

Cellfun: Executes the specified function for each cell of the cell array

Celldisp: Displays the contents of all the cells

Cellplot: Using graphical display of cellular arrays

Cell2mat: Transforming an array of cells into a normal matrix

Mat2cell: Transforming a numerical matrix into an array of cells

Num2cell: Converting a valarray into an array of cells

Deal: Assigning input parameters to the output

Cell2struct: Transforming a cell array into a structure

Struct2cell: Transforming a structure into a cellular array

Iscell: Determines whether the input is an array of cells





>> a={20, ' matlab ', 3-7i;ones (2,3), 1:3,0}

A =

[+] ' matlab ' [3.0000-7.0000i]
[2x3 Double]    [1x3 Double] [0]

>> b=cellfun (' Isreal ', a)

b =

1 1 0
1 1 1

>> c=cellfun (' length ', a)

c =

1 6 1
3 3 1

>> d=cellfun (' IsClass ', A, ' double ')

D =

1 0 1
1 1 1

(Application of functions)

The main function of the Cellfun function is to specify different functions for the elements of the cell array (cellular), but the function Ushuliang that can be used in the Cellfun function is limited.

Functions that can be used in Cellfun:

IsEmpty: If the cell element is empty, the return logic is true

Islogical: If the cell element is a logical type, it returns the logical

Isreal: If the cell element is a real number, it returns the logical true

Length: How long the cell element is

Ndims: The dimension of the cell element

Prodofsize: The number of elements contained in a cell element

(7) Nesting of cellular arrays

The cell in the cell array contains other cellular numbers, called nested cell arrays, and cells without nested structures are called page cells. You can create nested cell arrays with nested braces or cell functions, or directly with assignment expressions, and you can also access the elements of a subarray, cell, or cell of a nested cellular array.



>> (A=cell)

A =

[]     []

>> A (={cell) (2,2)}

A =

[] {2x2 cell}



>> A (={magic) (3)};
a{1,2} (={[1) 2 3;4 5 6;7 8 9]};
a{1,2} (2,1) ={[2-i;4+7i]};
a{1,2} (2,2) ={cell};
a{1,2}{2,2} (2) ={5};
>> Cellplot (a)

(8) Conversion between a cell array and an array of values

By applying a loop, you can convert an array of cells into a array of arrays of numbers.
Citation: http://hi.baidu.com/hbwc/blog/item/a89ec511ab669a1bb8127b5c.html

Matlab cell Array Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.