8
votes
$\begingroup$

I am just wondering if there is a list of the related Mathematica functions that correspond to Octave functions.

For example.

ones(n) maps to Array[1 &, n]

$\endgroup$
14
  • 3
    $\begingroup$ You might look for matlab-mathematica functions. But keep in mind that, as a language, mathematica is very different from matlab or octave. $\endgroup$
    – acl
    Commented Jul 22, 2014 at 12:29
  • 3
    $\begingroup$ I understand; like I said, you might have better luck looking for matlab-mathematica tables. Also, a more direct translation of ones(n) probably would be ConstantArray[1, n]. However, that creates a list (or vector) of Integer entries, which means that if you try to do any arithmetic with it it will be done with exact numbers. This is much slower than with reals. This sort of booby traps are everywhere in mma. $\endgroup$
    – acl
    Commented Jul 22, 2014 at 12:42
  • 3
    $\begingroup$ there are lots of problems solved by Matlab and Mathematica side-by-side here $\endgroup$
    – Nasser
    Commented Jul 22, 2014 at 14:30
  • 2
    $\begingroup$ I can certainly see why you are looking for such a list. I have wished for similar lists before. But beware: Mathematica and MATLAB are very different. Using such a list means that you are thinking in MATLAB/Octave and trying to impose that style on Mathematica, which is going to be a disaster. (I'm saying this based on personal experience when I was learning R, and R is close to Mathematica than Mathematica is to Octave.) $\endgroup$
    – Szabolcs
    Commented Jul 22, 2014 at 16:36
  • 2
    $\begingroup$ @Mr.Wizard As an example for the multiple outputs comment, consider sorting: res = sort(a) does the same as res = Sort[a], but [~, ind] = sort(a) is ind = Ordering[a] despite the function being called sort. And then [res, ind] = sort(a) is {res, ind} = Through[{Sort, Ordering}[a]] or {res, ind} = {a[[#]], #} &@Ordering@a, both of which would be too advanced for someone making the transition from MATLAB to Mathematica. $\endgroup$
    – rm -rf
    Commented Jul 22, 2014 at 18:08

1 Answer 1

10
votes
$\begingroup$

I closed this question because rm -rf convinced me that what this answer was intended to do is ultimately impossible: that there is simply no way to give an approximate one-to-one mapping of functions between Octave/Matlab and Mathematica; apart from a few limited cases any recommendations are going to be localized and opinionated rather than truly informative.

This answer may remain for the time being but it should be considered deprecated.


As far as I know there isn't one, so let's make one together. This is a Community Wiki post.

The idea is for one person to give an Octave/Matlab function of interest and describe what it does, then other users can recommend substitutes. The community can improve and curate those recommendations. An example entry:


ones

ones(n) builds an n-by-n array of ones

  1. Array[1 &, {n,n}]

  2. ConstantArray[1, {n,n}]

ones(n,1) builds an array of zeros of length n

As an alternative to the above

Range[n]^0

This is about as fast as ConstantArray and it's shorter. :-)


zeros

zeros(n,1) builds an array of zeros of length n

  1. Array[0 &, n]

  2. ConstantArray[0, n]

  3. Range[n] * 0

Also see ones above.


eye

eye(n) builds the identity matrix of dimension n

 IdentityMatrix[n]

for

for i=1:n
...
end
Do[..., {i, 1, n}]

Use Table instead of Do to collect results into a list. See also Array (and Function).


IMAGE PROCESSING

Basic Import and Export


imread('image.png') Read image from graphics file

 Import["image.png"]

imwrite(img,'image.png') Write image to graphics file

 Export["image.png","png"]

Image Type Conversion


rgb2gray(img) Convert RGB image or colormap to grayscale

ColorConvert[img, "Grayscale"]

$\endgroup$
17
  • $\begingroup$ The 3rd alternative for ones is a bit like this function for obtaining $2^n$: powerof2[n_] := Length@ReplaceRepeated[{0}, (0 -> Sequence[0, 0]), MaxIterations -> n] // Quiet $\endgroup$
    – acl
    Commented Jul 22, 2014 at 16:10
  • 1
    $\begingroup$ @Mr.Wizard Oh, I just found this on Wikipedia: "In fact, Octave treats incompatibility with MATLAB as a bug [...]". I think we are safe :) $\endgroup$
    – sebhofer
    Commented Jul 22, 2014 at 16:55
  • 2
    $\begingroup$ @Mr.Wizard Well, if we want to list all ways to replace MATLAB's for, it's going to take a while and will include Outer, Inner, Map, Apply, Cases, Select, and many more functions. That's exactly why I don't think it's a good idea to learn Mathematica based on such a list. $\endgroup$
    – Szabolcs
    Commented Jul 22, 2014 at 17:07
  • 2
    $\begingroup$ @Szabolcs I hope no one thinks this is the right way to learn Mathematica. Rather I hope it becomes a useful reference for those making the transition. I'm sure there are many possibilities for getting lost down the rabbit hole but that's what curation is for. $\endgroup$
    – Mr.Wizard
    Commented Jul 22, 2014 at 17:10
  • 1
    $\begingroup$ @Simon There's actually a Q&A for that alone; it happens to be mine: (7924) $\endgroup$
    – Mr.Wizard
    Commented Jul 22, 2014 at 17:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.