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

You can see that the general logic is the same but the syntax is different. Most importantly, MATLAB makes computations with arrays much easier.

 C code MATLAB code // declare a double precision variable // then assign a value   double a; a = 5.0; % variables are not declared and are % double precision by default   a = 5.0; // repeat which assigns values to // array elements   for(i=1; i<=10; i++)   A[i] = i; % i = start:increment:end % where increment = 1 is default   for i=1:10     A(i) = i; end // repeat which prints a series of // values   for(i=0; i<=10; i += 2)   printf("%i \n", i); for i=0:2:10     fprintf(' %i \n', i) end // initialize identity matrix     enum(N=100); double B[N]; for(r=0;r 3 \n"); } 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 // program which calls a function         #include % variables defined outside a % program unit are global y = 1; z = 2;   main(){   int b;     b = myfunc(3);     printf("%i \n", b); }     // function definition can be in // same file or separate file   int myfunc(int x){   int a;   a = x*y*z;   return(a); } % program and function definition must % be in separate files and function file % must have same name as function   global y z % declare global variables before % first use y = 1; z = 2;         b = myfunc(3)   % 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)   % function keyword, return variable %               = function name (inputs) % % 1st continuous group of % comments in a function are % displayed when you enter 'help' % and name of function in the command % window   global y z % declare global variables used % in this file   a = x*y*z; % return last value of return % variable in file % MATLAB functions can return an % array // 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 can // accumulate sum in each C[i][k]   for(i=0; i