MATLAB Examples

Contents

Basic operations

Define two int64 arrays

a = int64(1:10)'
b = int64(-5:4)'
a =

                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                   10


b =

                   -5
                   -4
                   -3
                   -2
                   -1
                    0
                    1
                    2
                    3
                    4

Test addition

a+b
ans =

                   -4
                   -2
                    0
                    2
                    4
                    6
                    8
                   10
                   12
                   14

Every operation also works with scalar values

a+1
ans =

                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                   10
                   11

Test subtraction

a-b
ans =

                    6
                    6
                    6
                    6
                    6
                    6
                    6
                    6
                    6
                    6

Test multiplication

a.*b
ans =

                   -5
                   -8
                   -9
                   -8
                   -5
                    0
                    7
                   16
                   27
                   40

Test division and absolute value

a./ (abs(b) + 1)
ans =

                    0
                    0
                    0
                    1
                    2
                    6
                    3
                    2
                    2
                    2

Powers

int64(2)^50
ans =

     1125899906842624

Test modulo

mod(a,2)
ans =

                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                   10

Bitshift

bitshift(int64(1),40)
ans =

        1099511627776

Matrix multiplication

Matrix multiplication is also supported

A = int64(100*rand(5))
B = int64(100*rand(5,3))

C = A*B
Cdouble = double(A)*double(B)
A =

                   91                   34                    5                   85                   69
                   49                   42                   22                   83                   65
                   79                   41                   79                    8                   27
                   91                   96                   18                   48                    0
                   86                   41                    5                   35                   45


B =

                   94                   73                   21
                   30                   38                   17
                   41                   55                   86
                   14                    1                   46
                   56                    3                   96


C =

                14833                 8502                13453
                11570                 6661                13693
                13519                11759                12110
                12844                11329                 7299
                12529                 8281                 8863


Cdouble =

       14833        8502       13453
       11570        6661       13693
       13519       11759       12110
       12844       11329        7299
       12529        8281        8863

Timing

Generate large matrices

A = int64(100*rand(400));
B = int64(100*rand(400));

Time the multiplication using int64

tic
C = A*B;
toc
Elapsed time is 0.343107 seconds.

Now try using double instead

A = double(A);
B = double(B);
tic
C = A*B;
toc
Elapsed time is 0.017866 seconds.

We see that using doubles is faster for big matrices, because it uses LAPACK, which is heavily optimized.