1. Building matrix:
>>rand(7) %7*7 matrix
>>rand(2,5) % 2*5 matrix
>>normrnd(0,1,[1 5])% random number 1*5 matrix from normal
%distribution with mean 0, standard deviation 1.
>> help rand %help manual
>>a=eye(6) %6*6 unit matrix
>>zeros(2,5) % 2*5 matrix with all elements 0
>>ones(7) % 7*7 matrix with all elements 1
>> b=[1; 2; 3;4;5;6] % a column vector with length 6
>> x=a\b % solve a*x = b
2. function
in stat.m
----------------------------------------
function [mean,stdev] = stat(x)
% to calculate mean and standard
%deviation of vector x
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
-----------------------------------------
>> b=[1; 2; 3;4;5;6]
>>[mean, stdev]=stat(b)
3. Format and misc
Matlab is case sensitive.
Matlab only display only 5 digits, using
>>format long
to display all 16 digits.
>>x=1; y=2; z=3
>>save filename
%will save x, y, z values to filename.mat
>>clear % clear x, y,z
>>load filename % will load filename.mat
>>whos % list x, y, z
>>save -ascii mydata.dat % save x, y and z in ascii
>>! vi mydata.dat % see what is inside mydata.dat
>>diary mydiary.out %will create diary to mydiary.out
>>diary on
>>diary off
4. Colon: create arrays or select a column from a matrix
>> x=-2:1 % -2 -1 0 1
>>x=-2:.5:1 % from -2 to 1 interval 0.5
>>a=rand(2,5)
>> a(2,:) % second row of a
>>a(:,3) % third column of a
>>a([1 2],:) %first and second column of a
>>[eye(2);zeros(2)] % combination of two matrices
5. if ... elseif ... else...end
in even.m
------------------------------
function b=even(n)
% b=1, even, b=0, odd
if mod(n,2)==0,
b=1;
elseif mod(n,2) ==1,
b=0;
else b=0;
end
--------------------------------
6.
For loops: for i=1:n, , end
in addmatrix.m
----------------------------------------------------------
function c=addmatrix(a,b)
% This is the function which adds
% the matrices a and b. It duplicates the MATLAB
% function a+b.
[m,n]=size(a);
[k,l]=size(b);
if m~=k | n~=l,
r='ERROR using add: matrices are not the same size';
return,
end
c=zeros(m,n);
for i=1:m,
for j=1:n,
c(i,j)=a(i,j)+b(i,j);
end
end
-----------------------------------------------------------
7. While loops: while , , end
in twolog.m
-------------------------------------------------
function l=twolog(n)
% l=twolog(n). l is the floor of the base 2
% logarithm of n.
l=0;
m=2;
while m<=n l=l+1; m=2*m;
end
----------------------------------------------------
8. Plot
x=-pi:0.1:pi y=sin(x)
plot(x,y, 'Color','g') %color green
hold on % show two plots in the same window
y1=cos(x)
plot(x,y1,'Color','r') %color red
xlabel('-\pi \leq \Theta \leq \pi') % x label theta between -pi and pi
ylabel('sin(\Theta)') % put ylabel
title('Plot of sin(\Theta)') % put title
------------------------------------------
in plotbar.m, plot data in bar
--------------
function plotbar
hold on;
x=35:1:47;
y1=[33,89,105,163,153,419,842,1197,1237,1144,641,269,117];
y2=[2,4,3,1,12,14,33,88,162,240,154,103,99];
h1=bar(x,y1,'g'); xlabel('weeks');
ylabel('cases');
h2=bar(x,y2,'b');
set(h1,'Displayname','confirmed'); % legend
set(h2,'Displayname','hospitalized'); %legend
set(get(gca,'YLabel'),'Rotation',0.0);
hold off;
--------------------------------------------------