-4

I have a mathematical data where it would be very convenient to have the index to start from zero like

a=sparse([],[],[],30,1);
>> a(0)=someValueHere
Subscript indices must either be real positive integers or logicals.

but Matlab by default offers only the index to start from 1. Is there some easy hack or trick by which I could still assign a(0) so that I don't need to create a dummyVar a0 for the value or append the value at the end?

So how to get assignment such as a(0) in Matlab? Every time zero-index called catch the error and return someValueHere instead of the warning?

hhh
  • 50,788
  • 62
  • 179
  • 282
  • Please, explain downvoting -- there must be some hack to get this working. I see no reason for not to: catch the error and redefine the assignment. – hhh Nov 20 '13 at 21:04
  • 1
    Very funny. `a(i+1)`? – horchler Nov 20 '13 at 21:04
  • @horchler Pardon. Trolling? – hhh Nov 20 '13 at 21:05
  • 5
    this is like asking how to redefine indexing in C to begin from, say, 42... I vote to close. – bla Nov 20 '13 at 21:06
  • 1
    Not at all. With `a(i+1)` now `i` can start at `0`. I don't know what other solution you might be looking for. Maybe write a mex function to use C's 0 indexing? I'm not sure you've explained your issue, which certainly can be solved using Matlab's indexing. – horchler Nov 20 '13 at 21:09
  • @natan I see it as an error-handling question but I am not sure whether it is a proper solution -- other solution is to create a dummy var -- not knowing which better alternative to get the zero index. – hhh Nov 20 '13 at 21:09
  • @horchler it is not a realistic solution for me because I am playing with binaries -- the zero index refers to specific monomial here constant and `0001` refers to a specific monomial also. Changing index numbers by one asks for problems, confusing things unnecessarily. – hhh Nov 20 '13 at 21:11
  • consider the possibility that your assignment might require that you start some variable 0, but not require that you index from 0. – bla Nov 20 '13 at 21:15
  • @natan I find it confusing. My partner solves this problem by declaring a dummy var A0 but I find it unnecessarily cluttered (it would require changing all functions with one extra parameter). I am trying to find a solution that would not require changing the indexing, far better for readability. I have large amount of vars such as 31 vars where each var correspond to a bit in binary so adding +1 to all indexing would require a larger data-structure by one-bit, err...not wanting one-bit longer data-structure... – hhh Nov 20 '13 at 21:18
  • 1
    possible duplicate of [Is zero based indexing available in MATLAB](http://stackoverflow.com/questions/4239907/is-zero-based-indexing-available-in-matlab) – marsei Nov 20 '13 at 22:13

2 Answers2

3

There is a discussion on the matlab index issue: http://www.mathworks.cn/matlabcentral/newsreader/view_thread/285566

Maybe you can write a function like

function t=C_index(x)
  t = x + 1;

Then you can write something like y(C_index(0)) to get the first value in vector y.

In Addition,

t=@(x) x+1
y(t(0)) 

should work.

bla
  • 25,846
  • 10
  • 70
  • 101
lennon310
  • 12,503
  • 11
  • 43
  • 61
3

To get MATLAB's index to start from 0 you'll need to make an large set of object classes that emulate regular numeric classes, but behave differently with functions such as subsassgn(), subsref() etc.

Maybe someone was crazy enough to do it somewhere, I'd expect this to take weeks to months of work to actually work properly.

bla
  • 25,846
  • 10
  • 70
  • 101