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<N;r++) for(c=0;c<N;c++) B[r][c] = 0.0; 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 double C[3] = {1,2,3};
|
C = [1 2 3]; % or C = [1, 2, 3] |
// declare and initialize an array double C[4] = {2,4,6,8}; |
% array name = [start:increment:end]; C = [2:2:8]; |
// print an array element on screen printf("%g \n", C[2]); |
C(2) % no ending ; displays value |
// declare and initialize an array // with fixed interval between values double C[4] = {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 enum{Rows=3, Cols=3}; double D[Rows][Cols] = {{1,2,3}, {4,5,6}, {7,8,9}}; |
% 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 printf("%g \n", D[2][2]); |
D(2,2) % row 2, column 2 |
// print selected sub array of 2D array for(r=1; r<=2; r++) printf("%g \n", D[r][1]); |
D(1:2,1) % rows 1 to 2 of column 1 |
// print all rows of column 1 of 2D // array for(r=1; r<Rows; r++) printf("%g \n", D[r][1]); |
D(:,1) % all rows of column 1 |
// logical expression double a = 1; double b = 2; if (a == 1 || b == 3) printf("a = 1 or b = 3 \n"); |
a = 1; b = 2; if a == 1 || b == 3 fprintf('a = 2 or b = 3 \n'); end |
// logical expresssion if a == 1 && b != 3 { printf("a=1 and b not 3 \n"); printf("OK? \n"); } |
if a == 1 && b ~= 3 fprintf('a=1 and b not 3 \n'); fprintf('OK?'); end |
// if structure if a != 1 fprintf('a is not 1 \n'); else if b != 3 fprintf('b is not 3 \n'); else fprintf('huh? \n'); |
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: printf("a+b = 1 \n"); break; case 2: printf("a+b = 2 \n"); break; case 3: printf("a+b = 3 \n"); break; default: printf("a+b > 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 <stdio.h> % 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<I; i++) for(k=0; k<K; k++) C[i][k] = 0;
// matrix multiplication, C = AB 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;
|