MATLAB Octave CLI: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 82: Line 82:
  C = zeros(2, 3)
  C = zeros(2, 3)
  C = ones(2, 3)
  C = ones(2, 3)
=Data Selection=
An element of a matrix:
<''Matrix-var-name''>(<''1-based-row-number''><''1-based-column-number''>)
Subset of a vector elements:
v(<''first-index''>:<''last-index''>)


=Variable Management=
=Variable Management=

Revision as of 18:46, 20 December 2017

Internal

Comments

% this is a comment

Prompt

PS1('>> ')

In-Line Operations

1 + 2
2 - 1
1 * 2, 2 * C % where C is a matrix
2 / 1
2 ^ 6 

Logical operations

Equality:

1 == 2

Non-equality:

1 ~= 2

Logical AND:

1 && 0

Logical OR:

1 || 0

Variable Assignment

Also see:

Variables

Scalar Variable Assignment

<scalar-variable-name> = <variable-value>

Vector Variable Assignment

Vector

Assignment and literal initialization:

<vector-variable-name> = [ v1 v2 v3 ... ] % An (1, n) vector
<vector-variable-name> = [ v1; v2; v3; ... ] % An (n, 1) vector

Other Types of Vector Initialization

Start from "start-value" (inclusively), increment with "increment-step" until the value is strictly larger than "end-value":

v = <start-value>:<increment-step>:<end-value>

Start from "start-value" to "end:value" with increments of 1:

v = <start-value>:<end-value>
v = zeros(1, 3)
v = zeros(3, 1)
v = ones(1, 3)
v = ones(3, 1)

Matrix Variable Assignment

Matrix

Assignment and literal initialization:

<two-dimensional-matrix-variable-name> = [ v11 v12 ... v1n;  v21 v22 ... v2n; ...; vm1 vm2 ... vmn ] % A (m, n) matrix.

Other Types of Matrix Initialization

C = zeros(2, 3)
C = ones(2, 3)

Data Selection

An element of a matrix:

<Matrix-var-name>(<1-based-row-number><1-based-column-number>)


Subset of a vector elements:

v(<first-index>:<last-index>)

Variable Management

who

Shows the name of the variable in the work space.

whos

Details about variables in the work space.

clear

Delete the names matching the pattern from the symbol table. The pattern is a simplified regular expression, with '?', '*', '[...]'. With no arguments, all user-define variables (local and global) are deleted.

Initialization Functions

zeros

v = zeros(1, 3)
C = zeros(2, 3)

ones

v = ones(1, 3)
C = ones(2, 3)

eye

Identity matrix

C = eye(3)

rand

v = rand(1, 3)
C = rand(2, 3)

randn

Gaussian.

v = randn(1, 3)
C = randn(2, 3)

Data Size

size

size()

Dimensions of a matrix:

size(A)

Returns a vector with two elements (number of rows and number of columns).

Number of rows of a matrix:

size(A, 1)

Number of columns of a matrix:

size(A, 2)

length

Should only be applied to vectors, for matrices is confusing.

Data Loading and Saving

load

Load the named variables from the file. If no variables are named, all variables found in the file will be loaded. Note that the file is in ASCII format, no variable names are saved in the file, so the variable that will be created will be named after the file (the basename of the file name).

load <file-name>
load('<file-name>')

save

save <file-name.mat> <variable-name>

Saves the data in a MATLAB format, and the variable name is also saved in the file.

To save the data as ASCII (the name of the variable is not saved in the file):

save <file-name.txt> <variable-name> -ascii

Printing

To print the value of the variable, simply type the name of the variable:

<variable-name>

disp

disp(<variable-name>)

Also see format.

printf

printf
fprintf
sprintf
a = pi;
sprintf('this is pi with 6 decimals %0.6f', a);

fprintf() is like printf(), just that the output is written to the FID stream instead of stdout.

Also see:

Generic printf

format

Reset or specify the format of the output produced by disp and the normal output mechanism.

Also see:

Display

short

Fixed point format with 5 significant figures in a field that is a maximum of 10 characters wide.

long

loose

Filesystem

cd

Modify the current directory.

pwd

Print working directory.

Miscellaneous

clc

Clears the terminal screen and move the cursor to the upper left corner.

close

Close figure windows.