Basic Learning of MATLAB -------------- cellular array

Source: Internet
Author: User

Similar to the architecture array, the cellular array can store data of different types and sizes. Each element is called a cell ).

(1) create a cellular array

Three methods: direct assignment, using the cell () function and using {} to directly create all cells in the cellular array.

Instance:

% 1. Direct value assignment method % assign a value to a unit in the unit array through the value assignment statement. Only one unit content can be created at a time. % There are two Assignment Methods: content Indexing Method and unit indexing method % (1) Content Indexing Method: enclose the following table in braces on the left side of the assignment statement, and add the unit content % instance on the right side: create a 2*2 cellular array c {} = 'this is a cell array'; % string c {} = [, 5, 0]; % matrix C {2, 1} = []; c {2, 2} = 4 + 3 * I; % plural class (c) % determined data type % running result: % ans = % cell % C = % 'This is a cell array' [2x5 double] % [] [4.0000 + 3.20. I] % (2) unit index method: Use () on the left of the value assignment statement to enclose the following table of units. The content of the Unit on the right is {} %: create a 2*2 cellular array C () = {'this is a cell array'}; % string C () = {[, 5; 6, 7, 0]}; % matrix C () = {[]}; C () = {4 + 3 * I }; % plural % same running result % 2. Cell () function creation method % cell () function can create a cell array, and you can also specify the size % Syntax: cell_array = cell (m, n) Create a cellular array of M * n, after definition, you can assign a value to it by using the value assignment method. 3. Use {} to directly create a cellular array. % to list the contents of the expected cellular array in sequence in braces. Separate them with commas, separate rows with semicolons. % instance: Create a 2*3 cell array cell = {['eas', 'is ', s', 'cellarray'], [1, 2], []; 2 + 3 * I, 4, eye (4)} class (cell) % running result: % cell = % 'thisscellarray' [1x2 double] [] % [2.0000 + 3.20. I] [4] [4x4 double] % ans = % cell
(2) access to the cellular array

There are two ways to access the % object array: % (1) enclose the subscript with () to access the structure type of the unit % (2) use {} to enclose the subscript and access the specific content of the unit. % compile the example based on the cell cellular array: Cella = cell) A = cell {1, 1} B = cell (1, 2) B = cell {2.0000} % running result % cell = % 'thisscellarray' [1x2 double] [] % [+ 3.20. I] [4] [4x4 double] % A = % 'thisscellarray' % A = % thisscellarray % B = % [1x2 double] % B = % 1 2% use celldisp () the function accesses all cell content in the cellular array. The result is: % cell {2.0000} = % thisisscellarray % cell {2%} = % 4% + 3.20. I % cell {} = % 1 cell {} = % cell {1, 3} = % [] % cell {2, 3 }=% 1 0 0 0% 0 1 0 0% 0 1 0% 0 0 0 1
(3) basic operations on the cellular array

Including access to the cellular array, extension, reduction, nesting, and other operations of the cellular array

% 1. The extension % of the cellular array extended instance cellcell {3, 2} = ['this is the extended content'] % automatically extended to 3*3 arrays, the unassigned part is [] % running result: % cell = % 'thisscellarray' [1x2 double] [] % [2.0000 + 3.20. I] [4] [4x4 double] % cell = % 'thisscellarray '[1x2 double] [] % [2.0000 + 3.20. I] [4] [4x4 double] % [] 'This is the extended content' [] % 2.% of the metacell array through delete the row or column % syntax of the array by assigning null values to the Unit to be deleted: % (1) Delete row: cellname (row number, :) = [] % (1) delete column: cellname (:, column number) = [] % instance cell (2, :) = [] % Delete cell (:, 1) = [] % Delete first column % result: % cell = % 'Thisscellarray' [1x2 double] [] % [] 'This is the extended content' [] % cell = % [1x2 double] [] % 'This is the extended content '[] % 3. The nested cellular array % The cellular array contains other cellular numbers, the meta-cell array is called the nested meta-cell array, and the meta-cell without the nested structure is called the page meta-cell. % Nested cell arrays can be created using nested braces or cell functions, or directly using value assignment expressions, % You can also access the child array, Meta cell, or meta cell element of the nested meta cell array. % Instance A = cell (); A {} = 12; A {} = []; % cellular array nesting A {} = 'cellular array nesting '; A {} = [, 3;, 6] cellplot () % plot the structure of the cell array % Result: % A = % [12] {1x1 cell} % 'nested cellular array' [2x3 double]

(4) operation functions of the cellular array

Celldisp () displays the CONTENT OF THE CELL ARRAY

Cellplot () plot the STRUCTURE OF THE CELL ARRAY

% Reshape () changes the shape of the cellular array reshape (A,) % based on the above example, the cellular array a is changed from 2*2 to 1*4%. Result: % ans = % [12] 'cell array nesting '{1x1 cell} [2x3 double] % cell2struct () convert the cell array to the schema array % num2cell () convert the Number Matrix to the cellular array num = [, 3;, 6]; cell_num = num2cell (Num) % digital matrix to the cellular array class (cell_num) % determine type iscell (cell_num) % determine whether it is a cellular array % run result: % cell_num = % [1] [2] [3] % [4] [5] [6] % ans = % cell % ans = 1% cellstr () convert a two-dimensional character array to a string unit array S = ['abc'; 'defg'; 'Hi']; C = cellstr (s) % result % C = % 'abc' % 'defg' % 'Hi' % class (c) % ans = % cell % char () converts a string cell array to a character array.


Basic Learning of MATLAB -------------- cellular array

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.