JGuizard
7/6/2016 - 6:41 PM

Matrix operations in Matlab/Octave

Matrix operations in Matlab/Octave

% define a matrix
m = [1 2;3 4;5 6]

% define a row vector
v = [1 2 3]

%define a column vector
v = [1;2;3]

% create a vector that start from 1, increment eache value from 0.1 till 2
v = 1:0.1:2

% create a matrix of ones
m = ones(3,3)
% create a matrix of zeros
m = zeros(3,3)
%create a matrix of random values
m = rand(3,3) %only >0
m = randn(3,3)
% create an magi matrix
m = magic(3)
% create an eye matrix
m = eye(3)
%flip a matrix
flipud(m)
%inverse of a matrix
pinv(m)


%show a colum/row of a matrix
m(2,:)
m(:,2)

%show a matrix as a single column vector
m(:)

%element wise matrix multiplication
m1 .* m2

%matrix transponse
A'

% max value in a matrix
max(m)
% max value for each row
max(m,[],1)
% find some values
find(m < 3)

%sum all columns in a matrix
sum(m,2)