Bookmark and Share

Matlab and Mathematica equivalent commands

Remarks:

I prepare matrices m1, m2, magic3, magic9 in Matlab and save them in files

>> m1=rand(3)

m1 =

    0.1386    0.8407    0.2435
    0.1493    0.2543    0.9293
    0.2575    0.8143    0.3500

>> m2=rand(3)

m2 =

    0.1966    0.4733    0.5853
    0.2511    0.3517    0.5497
    0.6160    0.8308    0.9172

>> save 'm1.dat' m1 -ascii

>> save 'm2.dat' m2 -ascii

>> magic3=magic(3)

magic3 =

     8     1     6
     3     5     7
     4     9     2

>> save 'magic3.dat' magic3 -ascii

>> magic9 = magic(9)

magic9 =

    47    58    69    80     1    12    23    34    45
    57    68    79     9    11    22    33    44    46
    67    78     8    10    21    32    43    54    56
    77     7    18    20    31    42    53    55    66
     6    17    19    30    41    52    63    65    76
    16    27    29    40    51    62    64    75     5
    26    28    39    50    61    72    74     4    15
    36    38    49    60    71    73     3    14    25
    37    48    59    70    81     2    13    24    35

>> save 'magic9.dat' magic9 -ascii

then import them in Mathematica:

In[67]:= SetDirectory["~/Desktop"];
In[143]:= m1=Import["m1.dat","Data"];
m2=Import["m2.dat","Data"];
magic3=Import["magic3.dat","Data"];
magic9=Import["magic9.dat","Data"];

The following comparison uses the variables defined above:

Matlab Mathematica remarks
"In Matlab, everything is a matrix." "In Mathematica, everything is an expression."
[] {} empty list
[1 2 3] {1, 2, 3}
[1; 2; 3] {{1}, {2}, {3}}
[1 2; 3 4] {{1, 2}, {3, 4}}
% this is a comment (* this is a comment *)
command1; command2; command1; command2; In both Mathematica and Matlab, ; indicates the end of a command and suppresses its output.
command1, command2 command1; command2 Matlab commands can be chained by ,; Mathematica commands can't be chained by ,.
'Hello world!' "Hello world!" Matlab uses single quote for string. Mathematica uses double.
% this is a comment (* this is a comment *)
command1; command2; command1; command2; In both Mathematica and Matlab, ; indicates the end of a command and suppresses its output.
command1, command2 command1; command2 Matlab commands can be chained by ,; Mathematica commands can't be chained by ,.
'Hello world!' "Hello world!" Matlab uses single quote for string. Mathematica uses double.
5:2:25 or [5:2:25] Range[5, 25, 2]
x(2:4) x[[2;;4]]
x(2, 3) x[[2, 3]]
x(:, 2) x[[All, 2]]
x(2, :) x[[2, All]]
x([1 3], :) x[[{1, 3}, All]]
x(1:3, :) x[[1;;3, All]]
x([1 3], [2 3]) x[[{1, 3}, {2, 3}]]
x(:, 2) = [1; 2; 3] x = Join[x[[{1}]], Transpose[{1, 2, 3}], x[[All, 3;;]], 2] Mathematica needs x = explicitly to get x updated
x(:) Flatten[Transpose[x]] Concatenate columns to one column.
y=x', y(:)' Flatten[x] Concatenate rows to one row.
m=[1, 2; 3, 4; 1, 1]; size(m) m={{1, 2}, {3, 4}, {1, 1}}; Dimensions[m] matrix dimension
[x [3; 5; 2]] Join[x, Transpose[{3, 5, 2}], 2] Append a column to a matrix, assuming there are 3 rows.
[x' [1; 6]' Append[x, {1, 6}] Append a row to a matrix, assuming there are 2 columns
[x y] Join[x, y, 2] Horizontally concatenate two matrices, assuming size(x, 1) equals size(y, 1) and Dimensions[x, 1] equals Dimensions[y, 1]
m1*m2 m1.m2 Matrix multiplication, assuming m1 is n × m matrix and m2 is m × p matrix.
m1 .* m2 MapThread[Times, {m1, m2}] Elementwise matrix multiplication, assuming m1 and m2 are n × m arrays.
m .^ 2 Map[#^2&, m, {2}] Elementwise exponentiation. Notice by virtue of Listability attribute of Power, Map[#^2&, m] which is equivalent to Map[#^2&, m, 1] also works.
exp(m) Exp[m] or E^m Elementwise exponentiation.
log(m) Log[m] Elementwise logrithm.
abs(m) Abs[m] Elementwise absolute value.
-m -m Elementwise negation.
ones(3, 4) Table[1, {3}, {4}] Matrix of ones.
m + 1 m + 1 Elementwise addition. m is a matrix.
m' Transpose[m] Matrix transpose.
pinv(m) Inverse[m] Matrix inverse.
sum(m) Total[Transpose[matrix], {2}] Columnwise sum.
sum(m,1) Total[Transpose[matrix], {2}] Columnwise sum.
sum(m,2) List/@Total[matrix, {2}] Row-wise sum.
prod(m) Apply[Times, m] or Times@@m Columnwise product. Times[{1, 3}, {2, 4}] has threading behavior.
floor(m) Floor[m] Elementwise flooring.
ceil(m) Ceiling[m] Elementwise ceiling.
max(m) Max /@ Transpose[m] Columnwise maximum.
max(m1, m2) Table[Max[m1[[i, j]], m2[[i, j]]], {i, Length[m1]}, {j, Length[Transpose[m1]]}] Elementwise maximum.
max(magic3, [], 1) Max /@ Transpose[magic3] Columnwise maximum.
max(magic3, [], 2) {Max[#]} & /@ magic3 Row-wise maximum.
max(max(magic3)) or max(magic3(:)) Max[magic3] Maximum matrix element.
[maxval idx] = max(m) maxval = Max /@ Transpose[m]; idx = MapThread[Position[##][[1,1]]&, {Transpose[m], Max /@ Transpose[m]}]
m < 2 Map[#<2&, m, {2}] Elementwise test. Notice {2} isn't neglegible as Less isn't Listable (Attributes[Less] doesn't contain Listable).
magic(3) A Mathematica program for constructing odd-order magic squares.
flipud(magic3) ? Flip matrix up/down direction.
fliplr(magic3) ? Flip matrix left/right direction.
magic3 = magic(3), find(m<5) Select[Flatten[Transpose[magic3]], #<5&] Find positions of elements less than 3.
[r,c] = find(magic3<5) {r, c} = {#[[All, 1, 1]], #[[All, 1, 2]]} &[Position[magic3, #] & /@ Cases[magic3, _?(# > 5 &), {2}]] Why the ordering of results are different?
eye(3) IdentityMatrix[3] Identity matrix.
rand() RandomReal[] or rand
rand(3) Table[RandomReal[], {3}, {3}] Create matrix with random reals.
x = rand(2, 3) x = RandomReal[{0, 1}, {2, 3}]
disp(m) or disp('hello') Print[m] or Print["Hello"]
who Names["*"]
whos ?
clear Clear[]
clear('x') Clear[x]
load('file.dat') Import["file.dat","Data"] file.dat contains tabulated numbers
save file.mat x DumpSave["file.mx", x]
save file.txt x --ascii Export["file.txt", "Data"]
save file.mat x DumpSave["file.mx", x]
matlab -nodesktop math (Linux), MathematicaKernel (Mac OS X, Windows), or MathematicaScript -script Non-GUI session.
MathematicaScript -script Scripting.
magic(3) A Mathematica program for constructing odd-order magic squares.
flipud(magic3) ? Flip matrix up/down direction.
fliplr(magic3) ? Flip matrix left/right direction.
magic3 = magic(3), find(m<5) Select[Flatten[Transpose[magic3]], #<5&] Find positions of elements less than 3.
[r,c] = find(magic3<5) {r, c} = {#[[All, 1, 1]], #[[All, 1, 2]]} &[Position[magic3, #] & /@ Cases[magic3, _?(# > 5 &), {2}]] Why the ordering of results are different?
eye(3) IdentityMatrix[3] Identity matrix.
rand() RandomReal[] or rand
rand(3) Table[RandomReal[], {3}, {3}] Create matrix with random reals.
x = rand(2, 3) x = RandomReal[{0, 1}, {2, 3}]
disp(m) Print[m] m is a matrix/expression.
fprintf('hello world!\n') Print["Hello world!"]`
find(m > 1) Positions[m, _?(#>1&)] find indices of elements of matrix m that is larger than 1
pack Share[] Consolidate memory.
who Names["*"]
whos ?
clear Clear[]
clear('x') Clear[x]
load('file.dat') Import["file.dat","Data"] file.dat contains tabulated numbers
save file.mat x DumpSave["file.mx", x]
save file.txt x --ascii Export["file.txt", "Data"]
save file.mat x DumpSave["file.mx", x]
matlab -nodesktop math (Linux) MathematicaKernel (Mac OS X, Windows), or MathematicaScript -script Non-GUI session.

Direct data download

blog comments powered by Disqus