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?
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.
MATLAB indices start from 1 (linear indexing), which is standard in mathematics (and matrix manipulation in particular).
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.
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.
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.
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?...
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. ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With