Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and variables 1.Representing tables as arrays in MATLAB 2.Concept of array dimension 3.Correspondence of array dimension to rows and columns 4.Picking.

Similar presentations


Presentation on theme: "Arrays and variables 1.Representing tables as arrays in MATLAB 2.Concept of array dimension 3.Correspondence of array dimension to rows and columns 4.Picking."— Presentation transcript:

1 Arrays and variables 1.Representing tables as arrays in MATLAB 2.Concept of array dimension 3.Correspondence of array dimension to rows and columns 4.Picking out individual items in an array 5.Picking out individual rows and columns 6.Creating variables to hold array values 7.Entering array data

2 Tabular data and arrays Tables are most common data representation MATLAB represents tables using arrays We will mainly work with 2D arrays (rows × columns) YearCases (in thousands) 1931 26.5 1932 10.3 1933 36.2 1934 5.0 1935 28.5 1936 36.5 193126.5 193210.3 193336.2 19345.0 193528.5 193636.5 Dim 2 (columns) Dim 1 (rows)

3 MATLAB arrays Name the array to access (e.g., assign to a variable) Access an item in the array by its position Row and column numbers start with 1 193126.5 193210.3 193336.2 19345.0 193528.5 193636.5 Dim 2 (columns) Dim 1 (rows) (1) (2) (1) (2) (3) (4) (6) (5) disease EXAMPLE: An array called disease represents a table of data. The first column contains the year, and the second column contains number of cases of the disease (in thousands) for that year. disease is a 6 x 2 array (it has 6 rows and 2 columns)

4 Picking out an item from a table Address items by name(row, col) Usually you create a new variable to hold value Make up a variable name that is meaningful Use assignment (=) to put a value in the variable 193126.5 193210.3 193336.2 19345.0 193528.5 193636.5 Dim 2 (columns) Dim 1 (rows) (1) (2) (1) (2) (3) (4) (6) (5) disease EXAMPLE: Designate the number of cases in 1931 (e.g., the value 26.5): disease(1, 2) EXAMPLE: Create a new variable holding number of cases in 1931: cases1931 = disease(1, 2);

5 Picking out row or column from a table Address items by name(row, col) Use : to designate “all rows” or “all columns” Make up a variable name that is meaningful Use assignment (=) to put a value in the variable 193126.5 193210.3 193336.2 19345.0 193528.5 193636.5 Dim 2 (columns) Dim 1 (rows) (1) (2) (1) (2) (3) (4) (6) (5) disease EXAMPLE: Designate column 2 of disease (e.g., all rows for column 2) disease(:, 2) EXAMPLE: Create a new variable for number of cases of the disease: cases = disease(:, 2);

6 Creating a table from some values Enter values left to right, top to bottom Use square brackets to enclose the values ([ ]) Commas separate values on same line Semicolons separate rows The... designates that code continues on next line 193126.5 193210.3 193336.2 19345.0 193528.5 193636.5 Dim 2 (columns) Dim 1 (rows) (1) (2) (1) (2) (3) (4) (6) (5) disease EXAMPLE: Create a variable for the first three rows of disease: myCases = [1931, 26.5;... 1932, 10.3;... 1933, 36.2];

7 Meaning of assignment (=) The statement A = B does NOT mean A and B are equal The left hand side (A) is a location designated by a variable The right hand side (B) is evaluated and assigned to the variable EXAMPLE: The assignment statement: cases = disease(:, 2); splits out the second column of array designated by disease and creates a new array designated by the cases variable holding a copy of the column.


Download ppt "Arrays and variables 1.Representing tables as arrays in MATLAB 2.Concept of array dimension 3.Correspondence of array dimension to rows and columns 4.Picking."

Similar presentations


Ads by Google