20

The indexing of an n-length array in C is from 0:N-1. whereas in MATLAB it is from 1:N

Now, I am more comfortable with the C-style indexing. Is there a way to tell MATLAB, by including some command in my MATLAB scripts or whatever, to adopt a C-style indexing rather than the traditional 1:N indexing?

MatlabDoug
  • 5,704
  • 1
  • 24
  • 36
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
  • 4
    As someone who regularly has to use MATLAB for one class, I can't tell you how much I wish this were possible. That and being able to do a `var++;` but alas, MATLAB has a place and that place isn't in serious non-mathematics programming. – Reese Moore Nov 21 '10 at 20:05
  • 1
    Might be an interesting task to add such a feature to MATLAB without breaking code. – zellus Nov 21 '10 at 20:24
  • 8
    @zellus - "Define interesting." ; "Oh god oh god we're all going to die" – Donnie Nov 22 '10 at 00:14

4 Answers4

11

No, and i believe the difference stems from the fact that mathematicians start counting from 1. (not that MATLAB is more suited for mathematicians, on the contrary it is used by engineers more (compared to Mathematica or Maple whose symbolic processing is more powerful))

If you want to code zero based, but similar to MATLAB, look at NumPy and SciPy, Python packages.

Also see Why numbering should start at zero for remarks on zero based vs one based indexing in general, and MATLAB indexing issue for a MATLAB specific discussion. See https://plus.google.com/115212051037621986145/posts/YTUxbXYZyfi for a discussion of this in Python.

ustun
  • 6,941
  • 5
  • 44
  • 57
  • Nice overview from wikipedia: http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array) – zellus Nov 21 '10 at 23:25
  • 6
    I am a mathematician and I count from 0. :( More seriously, mathematicians are used to indexing time by t=0,t=1,... etc. Matlab is used a lot for numerical analysis (differential equations) and the initial condition is always at 0. e.g. y(0) = initial value. – Legendre May 30 '13 at 20:05
  • 2
    [*Real mathematicians*](http://en.wikipedia.org/wiki/Ordinal_number) count from 0. – Yakov Galka Jul 09 '13 at 11:48
9

You could potentially do something like this by overloading the functions SUBSREF and SUBSASGN for all the different types of objects (built-in or user-defined) that you want to change the indexing scheme for. An example of one way to overload methods for built-in types is given in my answer to this question. The drawbacks?...

  • This would be a large and treacherous undertaking.
  • It would break all of the built-in functions that rely on one-based indexing, meaning you'd have to basically rewrite most of MATLAB.
  • Any code you might want to use from other MATLAB users, which would also rely on one-based indexing, would have to be rewritten.

In short, changing how built-in types handle indexing is not even remotely feasible. There is however another (albeit still somewhat treacherous) option making use of subclassing in MATLAB's OOP system. For example, you could make a new class double_zb that inherits from the built-in double class:

classdef double_zb < double
   methods
      function obj = double_zb(data)
         if nargin == 0
            data = 0;
         end
         obj = obj@double(data); % initialize the base class portion
      end
   end
end

Then you can extend double_zb with specialized implementations of SUBSREF and SUBSASGN that take zero-based indices. However, using double_zb objects instead of double objects effectively in your code may require you to either re-implement all the other methods for double objects or somehow implement converter methods for using double_zb objects with double methods. I'm not even sure of all the details involved in doing this, but I can certainly say that it would be a colossal headache.

My ultimate advice... stop worrying and learn to love the one-based indexing. ;)

Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 9
    Can't stop thinking about "The Sorcerer's Apprentice" by J.W. Goethe when reading this post. – zellus Nov 21 '10 at 23:27
  • As for overloading SUBSREF and SUBSASGN as pointed out in the beginning, couldn't one set a global logical variable every time one wants to use 0-based indexing, and have the overloaded SUBSREF and SUBSASGN check for that variable and use the classical 1-based indexing if it is not present? ;-) – arne.b Jul 28 '11 at 09:14
8

Actually, Matlab can use zero-based indices. They are indicated with an (unfortunately rather low precedence and rather obscurely documented) prefix operator "1+", i.e.

a(1+(0:m))

source: http://www.mathworks.com/matlabcentral/newsreader/view_thread/11510

I suppose the "1+" came from ppl living in one-base world: add one to make one-based index.

jobobo
  • 389
  • 5
  • 17
  • 10
    That's not zero-based indexing. The `1+` is simply adding a scalar value of 1 to all entries in the vector `(0:m)`, creating a one-based index `1:(m+1)` that is used to index `a`. – gnovice Jul 27 '11 at 19:04
  • 9
    True, but it's a trivial--yet useful--fix when you need your code to visually match a zero-based formula – acjay Feb 12 '12 at 20:56
4

I use Matlab for mathematics and we use "start at time = 0" a lot. My solution to this was to write a function:

time(x) = x + 1

For example, if I have an array y = [0,1,2,3,4] representing the values of y at time 0,1,2,3,4 respectively. I can do this:

y(time(0))

Which produces the correct result of 0. Thus avoiding having to modify all my equations.

Legendre
  • 3,108
  • 7
  • 31
  • 46