Skip to content
Willy Mills edited this page Nov 4, 2017 · 1 revision

MatLab

Tools

Language

Operations

Operation Syntax Note
row vector [1 2 3 4] or [1, 2, 3, 4]
2d matrix [1 2 3; 4 5 6; 7 8 9]
transpose a' (single quote)
inverse inv(a)
matrix multiply a * b
element-wise multiply a .* b
element-wise power a.^3
complex number 3 + 4i or 3 + 4j
indexing a(row, column) one based
inclusive index a(start:end) inclusive
generative start:step:end inclusive
generative start:end step=1
all a(3,:) all of third row
spread results [maxA, location] = max(A0 ]
comment a = 7 % comment is here

Loop and Branch

indexed loop for k=1:5 ... end
conditional if..elseif..else..end

Methods

Method Action
whos list workspace variables

Function Definitions

Single result

function result = myFunc(args)
   result = 3 * arg
end

Multiple results

function [x, y] = myFunc(args)
   x = 3 * arg
   y = 5 * arg
end

Anonymous Function

  sqr @(x) x.^0.5;