165 lines
2.6 KiB
Matlab
165 lines
2.6 KiB
Matlab
%VIM: let g:flags=""
|
|
%
|
|
a = 3
|
|
b = 'hi'
|
|
b
|
|
a = pi
|
|
c = (3 >= 1 )
|
|
disp(sprintf('2 decimals: %0.2f', a ))
|
|
format long
|
|
a
|
|
format short
|
|
a
|
|
|
|
A = [1 2; 3 4; 5 6] % define matrix
|
|
v = [1 2 3] % row vector
|
|
v = [1; 2; 3] % column vector
|
|
v = 1:0.2:2 % vector from 1 to 2 with 0.2 increments
|
|
v = 1:6 % vector 1 to 6
|
|
|
|
ones(2,3) % matrix of ones
|
|
2*ones(2,3)
|
|
zeros(3,1) % matrix of zeros
|
|
eye(4) % identity matrix
|
|
rand(2,3) % matrix of random numbers
|
|
randn(2,3) % matrix of normal distribution random numbers
|
|
|
|
w = -6 + sqrt(10)*randn(1,10000)
|
|
|
|
hist(w,50) % histogram
|
|
|
|
size(A) % sizes of matrix as a row vector
|
|
size(A,1) % number of rows
|
|
size(A,2) % number of columns
|
|
length(v) % length of a matrix
|
|
|
|
%load( 'file name' ) % load data from a file
|
|
|
|
who % what variables do I have.
|
|
whos % detailed who
|
|
|
|
%v = priceY(1:10)
|
|
%save savedfile.mat v
|
|
%save savedfile.mat v -ascii
|
|
|
|
clear % remove variables
|
|
|
|
A = [1 2; 3 4; 5 6 ]
|
|
A(3,2) % element on third row and second column
|
|
A(3,:) % third row
|
|
A(:,2) % second column
|
|
|
|
A([1 3], :) % matrix from 1 and 3 rows
|
|
A(:,2) = [10; 11; 12] % assign to second column
|
|
A = [A, [100; 101; 102]] % append to matrix
|
|
|
|
B = [ 3 3; 5 6; 6 7]
|
|
|
|
C = [ A B ] % append to the right
|
|
D = [ A; B ] % append at bottom
|
|
|
|
A' % transpos
|
|
A * B' % matrix mul
|
|
A .* B % element vice mult
|
|
A .^2 % element vice square
|
|
|
|
v = [ 1; 2; 3 ]
|
|
|
|
1 ./ v % reciprocal of v
|
|
1 ./ A
|
|
|
|
log(v) % element vice log
|
|
exp(v)
|
|
abs(v)
|
|
-v
|
|
v + ones(lenght(v), 1)
|
|
v + 1 % same as of above
|
|
|
|
a = [2 3 4 5]
|
|
max(a) % max element in a
|
|
[val, ind] = max(a) % val = max elem, ind = the index of elem
|
|
|
|
|
|
a < 3 % elem vice compare
|
|
find(a < 3)
|
|
|
|
A = magic(3)
|
|
|
|
[r,c] = find(A>=7)
|
|
|
|
sum(a)
|
|
prod(a)
|
|
floor(a)
|
|
ceil(a)
|
|
|
|
max( rand(3), rand(3) ) % elem vice max of two 3x3 matrices
|
|
|
|
max ( A, [], 1 ) % row vice max
|
|
max(A) % column vice max
|
|
|
|
A = magic(9)
|
|
|
|
sum(A,1)
|
|
|
|
A .* eye(9)
|
|
|
|
sum( sum( A.*eye(9))) % sum of diagonal elements
|
|
|
|
flipud( eye(9)) %flip up down
|
|
|
|
pinv(A) %pseudo inverse of A
|
|
|
|
|
|
%============================
|
|
t=[0:0.01:0.98];
|
|
y1 = sin(2*pi*4*t)
|
|
plot(t,y1);
|
|
y2 = cos(2*pi*4*t)
|
|
plot(t,y1);
|
|
hold on;
|
|
plot(t,y1,r);
|
|
xlabel('time')
|
|
ylabel('value')
|
|
legend('sin','cos')
|
|
title('my plot')
|
|
print -dpng 'myPlot.png'
|
|
close
|
|
|
|
|
|
figure(1); plot(t,y1);
|
|
figure(2); plot(t,y2);
|
|
subplot(1,2,1);
|
|
axis
|
|
|
|
A= magic(5);
|
|
imagesc(A);
|
|
imagesc(A),colorbar,colormap gray;
|
|
|
|
%============================
|
|
v = zero(10,1)
|
|
for i=1:10,
|
|
v(i) = 2^i;
|
|
end;
|
|
while i <= 5,
|
|
v(i) = 100;
|
|
i = i+1;
|
|
if i == 4, %elseif else
|
|
break;
|
|
end;
|
|
end;
|
|
|
|
%put into a separate file with the same name
|
|
function y = functionName( x )
|
|
y = x^2;
|
|
|
|
function [y1,y2] = functionName( x )
|
|
y1 = x^2;
|
|
y2 = x^3;
|
|
|
|
|
|
|
|
|
|
|
|
%help eye
|
|
|