MatlabMathematicaremarks "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](http://demonstrations.wolfram.com/MagicSquare/). `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](http://demonstrations.wolfram.com/MagicSquare/). `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.