Matlab > Matlab vs. other languages > Comparison of Javascript and MATLAB

 

You can see that the general logic is the same but the syntax is different. Most importantly, MATLAB makes computations with arrays easier. You can link to Javascript libraries for easier matrix operations - search web for Javascript matrix math library.

 

Javascript code

MATLAB code

// declare a variable then assign a value

// numeric variables are double precision by default

 

var a;

a = 5.0; // or combine: var a = 5.0;

 

% variables are not declared

% numeric variables are double precision by default

 

a = 5.0;

 

// repeat which assigns values to

// array elements

// array indices start at 0

 

for (i=0; i<10; i++)

  A[i] = i;

 

 

% array indices start at 1
% i = start:increment:end

% where increment = 1 is default

 

for i=1:10

   A(i) = i;

end

 

<!--repeat which prints a series of values

we have to display Javascript output in a web page -->

 

<html><body><p id="output"></p></body>

<script language="javascript">

var old;

for(i=0; i<=10; i+=2) {

  old = document.getElementById("output").innerHTML;

  document.getElementById("output").innerHTML =

    old + "<br>" + i.toString();

}

</script></html>

 

 

% use fprintf() function for

% formatted output to command window

 

for i = 0:2:10

  fprintf(' %i \n', i)

end

 

 

// initialize 100x100 identity matrix

 

var B = []; // declare 1D array

var N = 100;

for(r=0; r<N; r++) {

  B[r] = []; // add 1D array at each row, B now 2D

  for(c=0; c<N; c++)

    B[r][c] = 0;

}

// now put 1 into diagonal elements

for(r=0; r<N; r++)

  B[r][r] = 1.0;

 

% MATLAB has functions for

% common array initializations

 

B = eye(100);

 

// declare and initialize an array

 

var C = [1, 2, 3];

 

 

 

C = [1, 2, 3]; % or C = [1 2 3];

// declare and initialize an array

 

var C = [2, 4, 6, 8];

 

 

% array name = [start:increment:end];

C = [2:2:8];

 

// print an array element on screen

 

var C = [2, 4, 6, 8];

document.getElementById("output").innerHTML = C[1];

// displays 4

 

 

C = [2, 4, 6, 8];

C(2) % result displayed when no ending semicolon

% displays 4

 

// declare and initialize an array

// with fixed interval between values

 

var C = [2, 4, 6, 8];

 

 

C = linspace(2,8,4)

 

% third param is optional and = # points

% between and including 1st two points

% if third param left off, default is 100 points

 

// declare and initialize a 2D array

 

var D = []; // declare 1D array

// declare each element as additional 1D array

D[0] = [1,2,3];

D[1] = [4,5,6];

D[2] = [7,8,8];

 

 

% these three examples accomplish the

% same thing

D = [1 2 3; 4 5 6; 7 8 9];

D = [1:3; 4:6; 7:9];

D = [1 2 3

     4 5 6

     7 8 9];

 

// print element of 2D array

 

document.getElementById("output").innerHTML = D[1][1];

// displays 5

 

 

 

D(2,2) % row 2, column 2, displays 5

// print selected subarray of 2D array

 

var old;

for(r=0; r<2; r++) {

  old = document.getElementById("output").innerHTML;

  document.getElementById("output").innerHTML =

    old + "<br>" + D[r][0].toString();

}

// displays 1 and 4

 

 

 

 

D(1:2,1) % rows 1 to 2 of column 1

% displays 1 and 4

// print all rows of column 1 of 2D array

 

var old;

for(r=0; r < D.length; r++) {

  old = document.getElementById("output").innerHTML;

  document.getElementById("output").innerHTML =

    old + "<br>" + D[r][0].toString();

}

// displays 1, 4 and 7

 

 

 

D(:,1) % all rows of column 1

% displays 1, 4 and 7

// logical expression

 

var a = 1;

var b = 2;

if (a == 1 || b == 3) {

  document.getElementById("output").innerHTML =

    "a = 1 or b = 3";

}

 

 

 

a = 1;

b = 2;

if a == 1 || b == 3

  fprintf('a = 2 or b = 3 \n')

end

 

// logical expresssion

 

if (a == 1 && b != 3) {

  document.getElementById("output").innerHTML =

    "a=1 and b not 3";

}

 

 

 

if a == 1 && b ~= 3

  fprintf('a=1 and b not 3 \n')

end

 

// if structure

 

// note: use alert() here and below to save space vs.

// setting innerHTML as done above

 

if (a != 1) {

  alert('a is not 1');

} else if (b != 3) {

  alert('b is not 3');

} else {

  alert('huh?');

}

 

 

 

 

 

 

if a ~= 1

  fprintf('a is not 1 \n')

elseif b ~= 3

  fprintf('b is not 3 \n')

else

  fprintf('huh? \n')

end

 

// switch structure

 

switch (a+b) {

  case 1:

    alert("a+b = 1");

    break;

  case 2:

    alert("a+b = 2");

    break;

  case 3:

    alert("a+b = 3");

    break;

  default:

    alert("a+b > 3");

}

 

 

 

switch a+b

  case 1

    fprintf('a+b = 1 \n')

  case 2

    fprintf('a+b = 2 \n')

  case 3

    fprintf('a+b = 3 \n')

  otherwise

    fprintf('a+b > 3 \n')

end

 

// script which calls a function

 

// variables defined outside a program unit

// are global

var y = 1;

var z = 2;

var b = myfunc(3); // call function myfunc

alert("b = " + b.toString());

 

// function definition can be in

// same file or separate file

 

function myfunc(x) {

  var a = x*y*z;

  return a;

}

 

 

global y z % declare global vars before 1st use

y = 1;

z = 2;

b = myfunc(3) % call function myfunc

 

% function definition of myfunc

% must be in a separate file named

% myfunc.m (i.e., functionName.m)

 

LISTING OF FILE myfunc.m

 

function a = myfunc(x)

  global y z

  a = x*y*z;

  % returns last value of return variable in file

 

// matrix multiplication

// C = A*B

// where A is I rows by J columns

// B is J rows by K columns

// and C is I rows by K columns

 

// initialize matrix C so it can hold result

for(i=0; i<I; i++) {

  C[i] = [];

  for(k=0; k<K; k++) {

    C[i][k] = 0;

  }

}

 

// matrix multiplication, C = A*B

for(k=0; k<K; k++) {

  for(i=0; i<I; i++) {

    for(j=0; j<J; j++) {

      C[i][k] += A[i][j] * B[j][k];

    }

  }

}

 

 

C = A*B;