Is zero based indexing available in MATLAB
-
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?
-
Answer:
In a word: no...
smilingbuddha at Stack Overflow Visit the source
Other answers
You could potentially do something like this by overloading the functions http://www.mathworks.com/help/techdoc/ref/subsref.html and http://www.mathworks.com/help/techdoc/ref/subsasgn.html 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 http://stackoverflow.com/questions/2425251/how-do-i-get-real-integer-overflows-in-matlab-octave/2430278#2430278 to http://stackoverflow.com/questions/2425251/how-do-i-get-real-integer-overflows-in-matlab-octave. 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 http://www.mathworks.com/matlabcentral/fileexchange/, 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 http://www.mathworks.com/help/techdoc/matlab_oop/brgze9_-1.html. 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 http://www.mathworks.com/help/techdoc/ref/subsref.html and http://www.mathworks.com/help/techdoc/ref/subsasgn.html 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. ;)
gnovice
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 http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF for remarks on zero based vs one based indexing in general, and http://www.mathworks.cn/matlabcentral/newsreader/view_thread/285566 for a MATLAB specific discussion.
ustun
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
Related Q & A:
- Why is the size of an empty class not zero in C++?Best solution by geeksforgeeks.org
- What is the volume of an ideal gas at absolute zero?Best solution by Yahoo! Answers
- What is the best Zero skateboard deck?Best solution by skateboard.about.com
- How would I find the present value of a short position zero coupon bond?Best solution by Yahoo! Answers
- Is there a roman numeral for the number zero?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.