Contact: zeng  @  zegraph.com      Last update: September 2016

## Matrix Examples

### Example-1

```load("matrix.dll");             // load matrix library

csv("double matrix");
A=double(3,3);                  // create 3x3 matrix for C's double numbers
A.fill(1,1);                    // fill matrix with numbers
A.csv();                        // print matrix numbers

csv("int matrix");
B=int(5);                       // create 3x1 matrix for C's int numbers
B.fill(1,1);
B.csv();

csv("double frome int");
A=double(B);                    // create double matrix of same size and shape as B
A.csv();

csv("double with numbers");
A=double(1,2,3,4,5);            // create 3x1 double matrix and fill it with 1, 2, 3, 4, and 5.
A.csv();

csv("a generic way for matrix creation");
A=matrix("double",3,3);         // the same as A=double(3,3)
A.fill(0,1);
A.csv();
```

### Example-2

```load("matrix.dll");             // load matrix library

csv("source matrix");
A=int(5,5);                     // create 5x5 matrix for C's int numbers
A.fill(1,1);                    // fill matrix with numbers
A.csv();                        // print matrix numbers

csv("delete column");
A.delete(2);                    // delete the third column
A.csv();

csv("delete row");
A.delete(2,"r");                // delete the third row
A.csv();

csv("find");
[i,j]=A.find(10);               // find row and column indexes where A[i,j]=13
csv(i,j);

csv("find");
[i,j]=A.find(10,3,3);
csv(i,j);                       // i and j are null because no A element=13 after row 3 and column 3

csv("flip column");
A.flip();                       // flip columns
A.csv();

csv("row");
A.flip("r");                    // flip rows
A.csv();

csv("insert column");
A.insert(0,0);                  // insert zeros to the front.
A.csv();
A.delete(0);

csv("insert row");
B=int(2,4);
B.fill(0);
A.insert(1,B,"r");              // insert 2x4 matrix to the second row.
A.csv();

csv("pointer");
[ptr,n,e]=A.ptr();              // get pointer, size, and element size.
csv(ptr,n,e);

csv("resize");
A.resize(3,3);                  // resize
A.fill(1,1);
A.csv();

[nr,nc,nd]=A.size();            // get A's size;

csv("reshape");
A.reshape(1,nd);                // reshape A
A.csv();                        // see what happens?

csv("transpose");
A.reshape(nr,nc);               // transpose
A.trans();
A.csv();
```

### Example-3

```load("matrix.dll");            // load matrix library

A=double(5,4);
A.rand();
A.csv();

csv("sort");
A.sort();                      // sort A by the first column
A.csv();

csv("sort decending");
A.sort(1,false);               // sort A by the second column in decending order
A.csv();

csv(" ");
B=double(4,2);
B.fill(1,0);
B.csv();

csv("prod ");
A.prod(B);                     // B's row number must equal A's column number
A.csv();

csv("replace");
I=int(0,1,3,4);
B=double(0,0,3,4);
A.replace(I,B);                // replace some elements in A with B
A.csv();

csv("unique");
A.unique();                    // sort A by the first column
A.csv();
```

### Example-4

```load("matrix.dll");             // load matrix library

A=double(3,3);
A.rand();                       // fill A with random numbers

csv("default");
A.csv();

csv("customize digits");
A.csv("%0.2f");

csv("exponential format");
A.csv("%0.3e");

csv("print");
A.print("  %0.2f");

B=double(3,3);
B.fill(1,1);
[ptr,n]=B.ptr();

csv("import");
A.import(ptr);
A.csv();

csv("parse");
A.parse("1.e-1, 10, 3");
A.csv();

A.csv()
```

### Example-5

```load("matrix.dll");             // load matrix library

csv("source matrix");
A=double(2,2);
A.rand();                       // fill A with random numbers
A[0,0]=0;
A[0,1]=3.14159265358979/2.0;
A.csv();

csv("cos");
B=cos(A);
B.csv();

csv("acos");
B=acos(B);
B.csv();

csv("cosh");
B=cosh(B);
B.csv();

csv("sin");
B=sin(A);
B.csv();

csv("asin");
B=asin(B);
B.csv();

csv("sinh");
B=sinh(B);
B.csv();

csv("tan");
B=tan(A);
B.csv();

csv("atan");
B=atan(B);
B.csv();

csv("tanh");
B=tanh(B);
B.csv();

csv("sqrt");
B=sqrt(A);
B.csv();

csv("exp");
B=exp(A);
B.csv();

csv("ceil");
B=ceil(A);
B.csv();

csv("floor");
B=floor(A);
B.csv();

csv("abs");
B=-A;
B=abs(B);
B.csv();

csv("log");
A[0,0]=1;
B=log(A);
B.csv();

csv("log10");
B=log10(A);
B.csv();

csv("stat");
csv(A.min(),A.max(),A.sum(),A.mean(),A.stdev());
```

### Example-6

```load("matrix.dll");             // load matrix library

csv("source matrix");
A=double(2,);
A.fill(1,1);
A.csv();

csv("atan2(number,matrix)");
B=atan2(2,A);
B.csv();

csv("atan2(matrix,number)");
B=atan2(A,2);
B.csv();

csv("atan2(matrix,marix)");
B=atan2(A,A);
B.csv();

csv("pow(number,matrix)");
B=pow(2,A);
B.csv();

csv("pow(matrix,number)");
B=pow(A,2);
B.csv();

csv("pow(matrix,matrix)");
B=pow(A,A);
B.csv();
```

### Example-7

```load("matrix.dll");             // load matrix library

csv("source matrix");
//    year month day hour minute second
A=int(2001,3,    15, 21,  11,    5,
2008,5,    5,  1,   52,    1);
A.reshape(2,6);
A.csv();

csv("cal2jul");
B=cal2jul(A);
B.csv();

csv("jul2cal");
C=jul2cal(B);
C.csv();
```

### Example-8

```load("matrix.dll");

csv("source matrix");
A=int(3,3);
A.fill(-3,1);
A.csv();

csv("negate");
B=-A;
B.csv();

csv("transpose");
B=A~;
B.csv();

csv("increament");
A++;
A.csv();

csv("decreament");
A--;
A.csv();
```

### Example-9

```load("matrix.dll");

csv("double matrix");
A=double(3,3);
A.fill(1,1);
A.csv();

csv("matrix + number");
B=A+2;
B.csv();

csv("number + matrix");
B=2+A;
B.csv();

csv("matrix + matrix");
B=A+A;
B.csv();

// The above three operations behave
// the same for -, *, /, and %

csv("matrix += matrix");
A+=A;                       // better version for B=A+A;
A.csv();

csv("matrix += number");
A+=2;                       // better version for B=A+2;
A.csv();

// The above two operations behave
// the same for -=, *=, /=, and %=

csv("matrix == matrix");
I=A==A;
I.csv();

csv("matrix == number");
I=A==2;
I.csv();

csv("number == matrix");
I=2==A;
I.csv();

// The above two operations behave
// the same for !=, >, <, >=, <=, &&, and ||

csv("matrix | matrix");
B=A|A;
B.csv();

csv("matrix | number");
B=A|0;
B.csv();

// The above two operations behave the same for & as well.

csv("matrix << number");
B=A<<2;
B.csv();

csv("matrix >> number");
B=A>>1;
B.csv();
```

### Example-10

```load("matrix.dll");

csv("int matrix");
A=int(9);
A.fill(1,1);
A.csv();

csv("A[i]");
csv(A[3]);

I=A>5;
csv("char matrix");
I.csv();                // I is char matrix [0,0,0,0,0,1,1,1,1]

B=A[I];
csv("A[I]");
B.csv();
csv("A[A>5]");
B=A[A>5];
B.csv();

csv("A[b:e:s]");
B=A[1:7:2];
B.csv();

csv("A[Int]");
I=int(5);
I.fill(0,1);
I[0]=8;
B=A[I];
B.csv();```

### Example-11

```load("matrix.dll");

csv("int matrix");
A=int(5,5);
A.fill(1,1);
A.csv();

csv("A[i,j]");
csv(A[2,2]);            // get one value

csv("A[i,b:e]");
B=A[0,1:3];             // see what will happen with A[1:3,0]
B.csv();

csv("A[i,*]");
B=A[1,*];               // see what will happen with A[*,1]
B.csv();

csv("A[i,I]");
I=A[0,*]>=3;
B=A[1,I];               // see what will happen with A[I,1]
B.csv();

csv("A[*,I]");
I=int(3);
I.fill(4,-1);
B=A[*,I];               // see what will happen with A[I,*]
B.csv();
```

### Example-12

```load("matrix.dll");

csv("int matrix");
A=int(9);
A.fill(1,1);
A.csv();

csv("A[i]=v");
A[2]=0;
A.csv();

csv("A[I]=v");
A[A>5]=0;
A.csv();

csv("A[b:e]=v");
A[5:8]=1;
A.csv();

csv("A[I]=v");
I=int(3);
I.fill(8,-1);
A[I]=0;
A.csv();

csv("A[*]=v");
A[*]=0;
A.csv();
```

### Example-13

```load("matrix.dll");

csv("A");
A=int(9);
A.fill(1,1);
A.csv();

csv("B");
B=int(9);
B.fill(-1,-1);
B.csv();

csv("C");
C=int(1,2,3,4,5,6,7);
C.csv();

csv("A[I]=B mapping");
A[A>5]=B;
A.csv();

csv("A[I]=C filling");
A[A<0]=C;
A.csv();

csv("A[b:e]=B mapping");
A[3:5]=B;                   // see what will happen with A[3:5:2]=B
A.csv();

csv("A[b:e]=C filling");
A[3:5]=C;
A.csv();                   // see what will happen with A[3:5:2]=C

csv("A[*]=B mapping");
A[*]=B;
A.csv();
```

### Example-14

```load("matrix.dll");

csv("A");
A=int(5,5);
A.fill(1,1);
A.csv();

csv("A[i,j]=v");
A[1,2]=0;
A.csv();

csv("A[i,I]=v");
A[2,A[0,*]>3]=0;    // see what will happen with A[A[*,0]>3,2]=0
A.csv();

csv("A[i,b:e]=v");
A[0,0:3]=1;         // see what will happen with A[0:3,0]=1
A.csv();

csv("A[i,b:e:s]=v");
A[3,0:3:3]=1;
A.csv();

csv("A[i,*]=v");
A[3,*]=10;          // see what will happen with A[*,3]=1
A.csv();

A.fill(1,1);

csv("A[I,I]=v");
A[A[*,1]<10,A[0,*]>3]=0;
A.csv();

A.fill(1,1);

csv("A[I,b:e]=v");
A[A[*,0]>10,1:3]=0;  // see what will happen with A[1:3,A[0,*]>3]=0;
A.csv();

A.fill(1,1);

csv("A[I,*]=v");
A[A[*,0]>10,*]=0;   // see what will happen with A[*,A[0,*]>3]=0;
A.csv();

A.fill(1,1);

csv("A[b:e,b:e]=v");
A[0:3:2,1:2]=0;
A.csv();

csv("A[b:e,*]=v");
A[0:2,*]=0;     // see what will happen with A[*,0:2]=0;
A.csv();

csv("A[*,*]=v");
A[*,*]=0;
A.csv();
```

### Example-15

```load("matrix.dll");

csv("A");
A=int(5,5);
A.fill(1,1);
A.csv();

csv("B");
B=int(5,5);
B.fill(-1,-1);
B.csv();

csv("C");
C=int(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
C.csv();

csv("A[i,I]=B mapping");
A[2,A[0,*]>3]=B;    // see what will happen with A[A[*,0]>3,2]=B
A.csv();

A.fill(1,1);

csv("A[i,I]=C filling");
A[2,A[0,*]>3]=C;    // see what will happen with A[A[*,0]>3,2]=C
A.csv();

A.fill(1,1);

csv("A[i,b:e]=B mapping");
A[0,0:3]=B;         // see what will happen with A[0:3,0]=B
A.csv();

csv("A[i,b:e]=C filling");
A[0,1:3]=C;         // see what will happen with A[1:3,0]=C
A.csv();

A.fill(1,1);

csv("A[i,*]=B mapping");
A[3,*]=B;          // see what will happen with A[*,3]=B
A.csv();

csv("A[i,*]=C filling");
A[3,*]=C;          // see what will happen with A[*,3]=C
A.csv();

A.fill(1,1);

csv("A[I,I]=B mapping");
A[A[*,1]<10,A[0,*]>3]=B;
A.csv();

A.fill(1,1);

csv("A[I,I]=C filling");
A[A[*,1]<10,A[0,*]>3]=C;
A.csv();

A.fill(1,1);

csv("A[I,b:e]=B mapping");
A[A[*,0]>10,1:3]=0;  // see what will happen with A[1:3,A[0,*]>3]=B;
A.csv();

A.fill(1,1);

csv("A[I,b:e]=C mapping");
A[A[*,0]>10,1:3]=C;  // see what will happen with A[1:3,A[0,*]>3]=C;
A.csv();

A.fill(1,1);

csv("A[I,*]=B mapping");
A[A[*,0]>10,*]=B;   // see what will happen with A[*,A[0,*]>3]=B;
A.csv();

A.fill(1,1);

csv("A[I,*]=C filling");
A[A[*,0]>11,*]=C;   // see what will happen with A[*,A[0,*]>3]=C;
A.csv();

A.fill(1,1);

csv("A[b:e,b:e]=B mapping");
A[0:3:2,1:2]=B;
A.csv();

csv("A[b:e,b:e]=C filling");
A[0:3:2,1:2]=C;
A.csv();

A.fill(1,1);

csv("A[b:e,*]=B mapping");
A[0:2,*]=B;     // see what will happen with A[*,0:2]=B;
A.csv();

csv("A[b:e,*]=C filling");
A[0:1,*]=C;     // see what will happen with A[*,0:1]=C;
A.csv();

csv("A[*,*]=B mapping");
A[*,*]=B;
A.csv();
```

### Example-16

```load("matrix.dll");

A=double(0,0,1,0,1,1);      // a triagle
A.reshape(3,2);

csv("area");
csv(A.area());

csv("inside");
csv(A.inside(2,2));
csv(A.inside(0.5,0.5));
```

### Example-17

```load("matrix.dll");

//"Random data";
A=double(9,2);      // try A=double(9,3) for data on spherical surface.
A.rand();

[T,H]=A.delaunay();

csv("Triangles");
[m,n]=T.size();
for (i=0; i<m; i++) {
// the first vertex
k=T[i,0];
csv(A[k,0],A[k,1]);
// the second vertex
k=T[i,1];
csv(A[k,0],A[k,1]);
// the third vertex
k=T[i,2];
csv(A[k,0],A[k,1]);
k=T[i,0];
csv(A[k,0],A[k,1]);
csv("");
}

csv("Hull");
[m,n]=H.size();
for (i=0; i<m; i++) {
k=H[i,0];
csv(A[k,0],A[k,1]);
}
k=H[0,0];
csv(A[k,0],A[k,1]);

// Comment:
//    Save the results and plot them by Excel.
```

### Example-18

```load("matrix.dll");

//signal
A=double(30,1);
A.fill(0,.1);
A=5.0*cos(A);

// noise
B=double(30,1);
B.rand();

C=(A+B)|0;

csv("signal+noise");
C.csv();

csv("fft");
C.fft(1);
C.csv();

csv("inverse fft");
C.fft(-1);
C.csv();
```

### Example-19

```load("matrix.dll");

//intepo1d

XY=double(10,2);
XY.rand();
XY.sort();

csv("XY");
XY.csv();

X=double(10,1);
X.fill(0,.1);
csv("X");
X.csv();

Y=XY.interpo1d(X);
csv("XY interpo for X");
Y.csv();

//intepo2d

Zxy=double(5,5);
Zxy.rand();
csv("Zxy");
Zxy.csv();

X=double(4);
X.fill(.5,1);
csv("X");
X.csv();

Y=double(4);
Y.fill(.5,1);
csv("Y");
Y.csv();

Z=Zxy.interpo2d(X,Y);
csv("Zxy interpo for X,Y");
Z.csv();

X=double(5);
X.fill(.5,1);
csv("X");
X.csv();

Y=double(5);
Y.fill(.5,1);
csv("Y");
Y.csv();

Z=Zxy.interpo2d(X,Y,X,Y);
csv("interpo Z at U,U to at X,Y");
Z.csv();
```

### Example-20

```load("matrix.dll");

A=double(5,5);
A.rand();
csv("A ");
A.csv();

X=double(5,1);
X.fill(1,1);
csv("X");
X.csv();

csv("Y=A*X");
Y=A[*];
Y.prod(X);
Y.csv();

csv("A.invert()");
csv(A.invert());
A.csv();

csv("X=B*Y");
B=A[*];
B.prod(Y);
B.csv();
```

### Example-21

```load("matrix.dll");

// smooth

A=double(9,9);
A.rand();
csv("A");
A.csv();
csv("Smooth A");
A.smooth(3);
A.csv();

// sgsm

A=double(15,1);
A.fill(0,.3);
A=2.0*cos(A);

B=double(15,1);
B.rand();

C=A+B;

csv("signal+noise");
C.csv();

csv("sgsm");
C.sgsm(3,5);
C.csv();
```

### Example-22

```load("matrix.dll");

Y=double(15,1);
Y.fill(0,.3);
Y=2.0*cos(Y);

R=double(15,1);
R.rand();

Y+=R;

X=double(15,1);
X.rand(1000);
X.sort();

XY=X|Y;

csv("XY");
XY.csv();

csv("cvsp");
S=XY.cvsp(0.1,0.9,50);
S.csv();
```

### Example-23

```load("matrix.dll");

A=double(5,5);
A.rand();

[U,D,V]=A.svd();

csv("A");
A.csv();

csv("U");
U.csv();

csv("D");
D.csv();

csv("V");
V.csv();
```

### Example-24

```load("matrix.dll");

X=double(15,1);
X.rand();
X.sort();

Y=1.0+2.0*X+0.03*X*X;
X=X|X*X;
X.insert(0,1);

csv("X");
X.csv();

csv("Y");
Y.csv();

csv("mlfit");
csv(Y.mlfit(X));

csv("The first three values are coefficients");
Y.csv();
```

### Example-25

```load("matrix.dll");
// x
X=double(15,1);
X.rand();
X.sort();
// y = f(x)
Y=1.0+sqrt(2.0+5.0*X*X);
// two column matrix
XY=X|Y;
// initial guess of parameters
P=double(2.0,3.0,4.0);
// fitting
[a,b]=XY.nlsim("f",P);      // see what heppans with [a,b]=XY.nllm("f",P)
// check results
csv(a,b);
P.csv();
// call back function to be used by nlsim
function f(x, p)
{
R=p[0]+sqrt(p[1]+p[2]*x*x);
return R;
}
```

### Example-26

```load("matrix.dll");

Y=double();
csv(Y.size());
L=60;

[U,D,J]=Y.ssa(L);
D.csv();
J.csv();

Y1=Y.ssa(U,J[1]);
Y2=Y.ssa(U,J[10]);
csv(Y1.ssa(Y2,L));

Y2=double(Y);
Y2.fill(0,0);
for (i=0; i<L; i++) {
A=Y.ssa(U,i);
Y2+=A;
}
Y2.csv();
```