Arman Akbarian
UNIVERSITY OF BRITISH COLUMBIA
PHYSICS & ASTRONOMY DEPT.

%simple arithmatics in matlab:
>>7+2/3
ans =  7.6667

>>format long

>>1/2
ans =  0.500000000000000

>>3^0.2
ans =  1.24573093961552

%assignment statement:
>>a = 3
a =  3

>>b = a + 6
b =  9

%built-in functions:
>>exp(2)
ans =  7.38905609893065

>>log(2.3)
ans =  0.832909122935104

>>log10(100)
ans =  2

>>nthroot(80,3)
ans =  4.30886938006377

>>sin(pi/6)
ans =  0.500000000000000

>>eps
ans =  2.22044604925031e-16

>>i
ans =  0 + 1i

>>j
ans =  0 + 1i

>>clear a
>>a
error: `a' undefined near line 18 column 1
>>who

*** dynamically linked functions:

builtin:find  cellfun       dispatch

*** currently compiled functions:

findstr   ispc      mod       pkg       strrep
fullfile  isscalar  nthroot   strcat

*** local user variables:

__nargin__  ans         b

>>%This is a comment!

%vector definitions:
>>RowVec1=[1 8 73 2.3]
RowVec1 =

    1.00000000000000    8.00000000000000   73.00000000000000    2.30000000000000

>>format short
>>VerticalVec1=[1;2.3;4;11]
VerticalVec1 =

    1.0000
    2.3000
    4.0000
   11.0000

%operators are alywas vector/matrix based:
>>RowVec1*VerticalVec1
ans =  336.70
>>x=[1:2:9]
x =

   1   3   5   7   9

>>y=[-3:4]
y =

  -3  -2  -1   0   1   2   3   4

>>z=linspace(0,1,11)
z =

 Columns 1 through 8:

   0.00000   0.10000   0.20000   0.30000   0.40000   0.50000   0.60000   0.70000

 Columns 9 through 11:

   0.80000   0.90000   1.00000

>>MatrixA=[1 2 3; 2 4 4; 9 3 1]
MatrixA =

   1   2   3
   2   4   4
   9   3   1

>>B=[1:2:11; 0:5:25; linspace(10,60,6)]
B =

    1    3    5    7    9   11
    0    5   10   15   20   25
   10   20   30   40   50   60

>>C=ones(2,2)
C =

   1   1
   1   1

>>D=zeros(3,4)
D =

   0   0   0   0
   0   0   0   0
   0   0   0   0

>>DiagI=eye(5)
DiagI =

   1   0   0   0   0
   0   1   0   0   0
   0   0   1   0   0
   0   0   0   1   0
   0   0   0   0   1

>>A=[1 2; 3 4]
A =

   1   2
   3   4

>>B=A'
B =

   1   3
   2   4

>>C=inv(A)
C =

  -2.00000   1.00000
   1.50000  -0.50000

>>C*A
ans =

   1.00000   0.00000
   0.00000   1.00000

>>det(A)
ans = -2
>>A(1,2)
ans =  2
>>x=[1 2 7 3 8 3 2 1]
x =

   1   2   7   3   8   3   2   1

>>d=x(3:6)
d =

   7   3   8   3

>>A = [1:2:11; 2:2:12; 3:3:18; 4:4:24; 5:5:30]
A =

    1    3    5    7    9   11
    2    4    6    8   10   12
    3    6    9   12   15   18
    4    8   12   16   20   24
    5   10   15   20   25   30

>>B=A(:,3)
B =

    5
    6
    9
   12
   15

>>C=A(1,:)
C =

    1    3    5    7    9   11

>>D=A(1:3,2:4)
D =

    3    5    7
    4    6    8
    6    9   12

>>x=[1 3 2 4]
x =

   1   3   2   4

>>length(x)
ans =  4
>>A=[1 3 2 3; ones(1,4)]
A =

   1   3   2   3
   1   1   1   1

>>size(A)
ans =

   2   4

>>B=[A;A]
B =

   1   3   2   3
   1   1   1   1
   1   3   2   3
   1   1   1   1

>>A = [1 2 3; 2 1 3];
>>B = [0 0.1 1/3; 1 3 3];
>>C= A .* B
C =

   0.00000   0.20000   1.00000
   2.00000   3.00000   9.00000

>>C = A ./ B
C =

        Inf   20.00000    9.00000
    2.00000    0.33333    1.00000

>>A .^ 2
ans =

   1   4   9
   4   1   9

>>x=[1:10]
x =

    1    2    3    4    5    6    7    8    9   10

>>y=x .^2 + 2*x
y =

     3     8    15    24    35    48    63    80    99   120

%operations on all the vector:

>>z=sqrt(y)
z =

 Columns 1 through 8:

    1.7321    2.8284    3.8730    4.8990    5.9161    6.9282    7.9373    8.9443

 Columns 9 and 10:

    9.9499   10.9545

>>max(z)
ans =  10.954
>>A=rand(2,3)
A =

   0.798416   0.840041   0.646220
   0.071128   0.717128   0.306657
%reduction operators:

>>max(A)
ans =

   0.79842   0.84004   0.64622

>>min(A)
ans =

   0.071128   0.717128   0.306657

>>sum(A)
ans =

   0.86954   1.55717   0.95288

>>sum(x)
ans =  55
>>G=rand(2,2,2)
G =

ans(:,:,1) =

   0.11344   0.97663
   0.78475   0.76265

ans(:,:,2) =

   0.96071   0.94708
   0.38121   0.87918



>>max(G)
ans =

ans(:,:,1) =

   0.78475   0.97663

ans(:,:,2) =

   0.96071   0.94708



>>max(max(G))
ans =

ans(:,:,1) =  0.97663
ans(:,:,2) =  0.96071


>>max(max(max(G)))
ans =  0.97663
>>%so max is reduction operator along the most last index

>>A=rand(2,3)
A =

   0.24015   0.78117   0.88353
   0.49747   0.59078   0.41497

>>sort(A)
ans =

   0.24015   0.59078   0.41497
   0.49747   0.78117   0.88353

>>x=rand(1,3)
x =

   0.011435   0.105097   0.625881

>>y=rand(1,3)
y =

   0.29051   0.78338   0.75266

>>u=cross(x,y)
u =

  -0.411201   0.173215  -0.021573

>>u=dot(x,y)
u =  0.55673


last update: Wed May 11, 2016