58 lines
1.6 KiB
Matlab
58 lines
1.6 KiB
Matlab
clear
|
|
|
|
% scalar and vectors are matrices
|
|
a = 13;
|
|
assert(a(1,1) == 13);
|
|
|
|
v = [1 2 3]; % vectors are row vector by default
|
|
t = [4,5,6]; % can also use comma to separate values
|
|
assert(v(1,2) == 2);
|
|
assert(v(2) == 2);
|
|
t_col = t.'; % column vector given by transpose
|
|
assert(t_col(3, 1) == 6);
|
|
|
|
% complex transpose (adjoint matrix)
|
|
s = [1+1i 3-1i];
|
|
assert(all(s' == [1-1i 3+1i].'));
|
|
|
|
% size() finds the matrix dimensions of something
|
|
assert(all(size(a) == [1 1]));
|
|
assert(all(size(v) == [1 3]));
|
|
assert(all(size(v') == [3 1]));
|
|
|
|
% length() returns the largest dimension given by size()
|
|
assert(length(a) == 1);
|
|
assert(length(v) == 3);
|
|
assert(length(v.') == 3);
|
|
|
|
% isempty() is equivalent to length(X) == 0
|
|
assert(isempty(a) == false);
|
|
assert(isempty([]) == true);
|
|
|
|
% ndims() is equivalent so length(size(v))
|
|
% this proves that scalars and vectors are matrices
|
|
assert(ndims(a) == 2);
|
|
assert(ndims(v) == 2);
|
|
assert(ndims(v.') == 2);
|
|
|
|
% create a 3x3 matrix: rows separated by ;
|
|
M = [1 2 3; 4 5 6; 0 -1 -2];
|
|
|
|
assert(M(2, 3) == 6);
|
|
all(M' == [1 4 0; 2 5 -1; 3 6 -2]); % all operates on the first dimensions
|
|
% returns [1 1 1];
|
|
% check all elements (i.e. all dimensions) by doing
|
|
assert(all(M.' == [1 4 0; 2 5 -1; 3 6 -2], 'all'));
|
|
% can be done better by doing
|
|
assert(isequal(M.', [1 4 0; 2 5 -1; 3 6 -2]));
|
|
|
|
% note: length on a matrix is not the number of elements
|
|
% but the size of the max dimension
|
|
assert(length(M) == 3);
|
|
|
|
% numel() is equivalent to multiplying the entries given by size()
|
|
% returns the number of scalar elements
|
|
assert(numel(a) == 1);
|
|
assert(numel(v) == 3);
|
|
assert(numel(M) == 9);
|