Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is zero based indexing available in MATLAB

Tags:

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?

like image 587
smilingbuddha Avatar asked Nov 21 '10 19:11

smilingbuddha


People also ask

What kind of indexing is used in MATLAB?

In MATLAB®, there are three primary approaches to accessing array elements based on their location (index) in the array. These approaches are indexing by position, linear indexing, and logical indexing.

Do MATLAB indices start at 0 or 1?

MATLAB indices start from 1 (linear indexing), which is standard in mathematics (and matrix manipulation in particular).

Why do we use 0 based indexing?

Zero-based indexing is a very common way to number items in a sequence in today's modern mathematical notation. In particular, the combinatorial number system uses the numbers 0 to represent empty subsets of a set when it comes to combinatorics.

Are arrays indexed from 0?

In computer science, array indices usually start at 0 in modern programming languages, so computer programmers might use zeroth in situations where others might use first, and so forth.


2 Answers

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.

like image 186
ustun Avatar answered Sep 20 '22 03:09

ustun


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. ;)

like image 21
gnovice Avatar answered Sep 21 '22 03:09

gnovice