See also scribbles from 26 June 2013
Direct Entry
- [ list ] - specify values. Comma to separate numbers, semicolon to separate rows.
- Trying to READ an element of an array that is out of bounds will generate an error, but WRITING to one will simply expand the array to accommodate the new data ( so long as the indices are positive. ) For example:
data = ones( 2, 2 ); % Creates a 2 x 2 array of value 1 whos x = data( 3, 3 ); % Error - element doesn't exist. whos data( 3, 3 ) = 42 % Sets the value and expands the array, filling with zeros. whosColon Operator
- Colon operator - Fill in using a given increment ( 1 by default)
- X = 1 : 10;
- Y = 100 : 20 : 500;
- Z = 0 : pi / 10 : pi;
- down = 10 : -1 : 1;
Subset or Combination of other arrays
- X = Y( 2:3, 5:7 ); % rows 2 to 3 and columns 5 to 7 of Y, yielding 2 rows by 3 columns result in X
- The colon operator can also be used as a wildcard to specify either all rows or all columns:
- X = Y( :, 4 ); % X gets column 4 of Y, ( all rows )
- X = Y( 5:end, : ); % X gets all rows of Y from row 5 to the last row, all columns
- Can also concatenate matrices.
- A = [ B C ]; % B and C must have the same number of rows.
Using Matlab Functions
- linspace, logspace - Fill in linearly or logarithmically using endpoints and number of points
- Functions ( see below ) - eye, magic, peaks, rand, diag, etc.
- Matrices may be "tiled" using "repmat( array, nRows, nCols )"
- zeros
- ones
- eye
- linspace
- logspace
- repmat
- See "help elmat" for a full list
Load from a file
- Data files may be imported into Matlab variables by right-clicking on them in the directory listing, or by using the load( ) command.
- Just specifying load( filename ) will create a Matlab variable with the same name as the file.
- variableName = load( filename ) will create the variable with the name specified.
- Data files can be either .mat format ( saved earlier from Matlab ), or plain text ascii files, such as might have been generated from a text editor or another program.