8

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]

Simon O'Doherty
  • 721
  • 4
  • 16
  • 3
    You might look for matlab-mathematica functions. But keep in mind that, as a language, mathematica is very different from matlab or octave. – acl Jul 22 '14 at 12:29
  • It's more about understanding what I should type in Mathematica vs Octave, rather then a direct port of scripts. – Simon O'Doherty Jul 22 '14 at 12:38
  • 3
    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. – acl Jul 22 '14 at 12:42
  • 3
    there are lots of problems solved by Matlab and Mathematica side-by-side here – Nasser Jul 22 '14 at 14:30
  • 2
    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.) – Szabolcs Jul 22 '14 at 16:36
  • We shall need a way to organize the CW post below. For the time being I propose simple alphabetical order by Octave/MatLab function name. Any objections? – Mr.Wizard Jul 22 '14 at 17:27
  • 1
    @Mr.Wizard I object because I think this is futile... Having used both MATLAB and Mathematica extensively, I'll second Szabolcs in saying that the two are sufficiently different in feel and functionality that compiling such a list would 1) mostly consist of trivial mappings like cos -> Cos 2) some more like ones, eye, etc. which might not be idiomatic in Mathematica. I think this will only encourage horrible programming practices in Mathematica. Also, why not Python + Mathematica, Java + Mathematica, and so on? I would probably suggest closing this question... – rm -rf Jul 22 '14 at 17:44
  • @rm-rf Now I'm torn about it; I wish to defer to your experience, yet early indication is that this is a fairly popular idea. Although I wouldn't rule out "Python + Mathematica" the reason I thought this might be useful is that I believe the target audiences of Octave/Matlab and Mathematica intersect fairly broadly. Are there really so few functions in Matlab, or are they so different from Mathematica, that only a few translations are applicable? – Mr.Wizard Jul 22 '14 at 17:55
  • @Mr.Wizard I think that once you move past the basic single input-single output functions, you'll realize that there are a lot more differences — MATLAB has a single input-many outputs functionality that would be confusing to a Mathematica programmer. One of my early questions was about mimicking this (although, my question was more academic in intent and to populate the site during the private beta). There really is no equivalent to this in Mathematica (and for good reason) and you won't be able to draw parallels without causing more confusion. – rm -rf Jul 22 '14 at 18:01
  • @Mr.Wizard Then there are cases like this where both have specialized functions, yet use different definitions. In the end, I personally feel that there is no substitute for programming idiomatically in each system and that comes only with practice and reading the documentation. – rm -rf Jul 22 '14 at 18:02
  • 2
    @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. – rm -rf Jul 22 '14 at 18:08
  • 1
    @rm-rf Ouch. I see your point. Let me ask you this: how can we (general) best help Matlab users who are interested in transitioning? I for one can't read Matlab code, and I think posts have been closed when someone posted Matlab code asking for a translation. Therefore is the best (or even only) simply to teach Mathematica from the ground up as we have been doing? – Mr.Wizard Jul 22 '14 at 18:54
  • 1
    @Mr.Wizard I think so... teaching someone to forget a particular way of thinking is much harder and in my experience, bulk of the effort must come from their end. I think that what we've been doing so far is a good strategy — user posts short MATLAB snippet, explaining what it does and also posts their best effort Mathematica translation; someone posts an answer explaining the idiomatic way of doing things. – rm -rf Jul 22 '14 at 19:26
  • @rm-rf I suggest you close this but not delete the CW. That way we discourage using this but at the same time don't block people from using it. (Edits to CW posts of closed questions are still possible I believe?) – Mr.Wizard Jul 22 '14 at 19:30

1 Answers1

10

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"]

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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 – acl Jul 22 '14 at 16:10
  • @acl or maybe like this one in V10 PowerRange[1, 100, 2] – RunnyKine Jul 22 '14 at 16:16
  • @RunnyKine not baroque enough – acl Jul 22 '14 at 16:17
  • @acl. I agree :) – RunnyKine Jul 22 '14 at 16:17
  • @Mr.Wizard Maybe we should make it Octave/Matlab. I think the function names are mostly the same anyway but I'm not 100% sure and couldn't find anything relevant right now. So I guess, if the functions have different names, just name both. What do you think? – sebhofer Jul 22 '14 at 16:25
  • @acl Surely you jest. Range[n]^0 is actually quite useable. :-p – Mr.Wizard Jul 22 '14 at 16:40
  • @sebhofer Frankly I'm not familiar enough with these systems to make that judgement. If they have nearly the same set of functions (sometimes with different names) I don't see why not. If however the functions do subtly different things it could turn out to be a real pain to maintain. – Mr.Wizard Jul 22 '14 at 16:42
  • 1
    @Mr.Wizard Oh, I just found this on Wikipedia: "In fact, Octave treats incompatibility with MATLAB as a bug [...]". I think we are safe :) – sebhofer Jul 22 '14 at 16:55
  • 2
    @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. – Szabolcs Jul 22 '14 at 17:07
  • This is awesome, although I'd recommend to tell people not to use loops. See: http://mathematica.stackexchange.com/a/18396/16072 – Simon O'Doherty Jul 22 '14 at 17:09
  • 2
    @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. – Mr.Wizard Jul 22 '14 at 17:10
  • 1
    @Simon There's actually a Q&A for that alone; it happens to be mine: (7924) – Mr.Wizard Jul 22 '14 at 17:12
  • @Mr.Wizard I just realized that your first example is not correct (so is the zeros one): ones(n) creates a n-by-n matrix of 1s! The equivalent to your commands is ones(1,n) or ones(n,1) depending on the interpretation. – sebhofer Jul 23 '14 at 09:00
  • @sebhofer Why don't you edit this post to give the proper definition for ones, then I'll modify the recommendation(s) to match. Also, rm -rf argues compellingly that this question should be closed so you may not want to bother; your call. – Mr.Wizard Jul 23 '14 at 09:13
  • @Mr.Wizard Will do, I just wasn't sure what you'd want to do with solution 3 :) Anyway, I agree that it is probably really hard to maintain this list in a sensible way. MATLAB is heavily overloaded and Mathematica is too. – sebhofer Jul 23 '14 at 09:20
  • @Mr.Wizard Programming wise yes there are many flexibilities in Mathematica and hard to map one by one. However, toolboxes such as image possessing, statistics, wavelets, ect. has very similar commands. It may need a lot of effort to map, but as community page one can improve and add thing as common effort. I personally think it will be quite beneficial for future 2 program users. – s.s.o Jul 23 '14 at 10:34
  • @s.s.o Okay. Let's proceed on that basis, and if after a while it appears productive I will reopen this. – Mr.Wizard Jul 23 '14 at 10:43