|
"Royi Avital" <RoyiREMOVEAvital@yahoo.com> wrote in message <hea0q5$phk$1@fred.mathworks.com>...
> I wish Matlab would number the first cell in a vector with the '0'.
>
> Yet, Matlab counting (Of the cells number) starts with '1'.
>
> Hence you can't access cell with Vector_Name(0) as you do in your code.
Well, that wasn't the only problem. The expression 'n-k' was generating all kinds of other out of bounds indices as well.
In any case, if you really want to be able to index matrices starting from 0, you can make your own data type, as I've done for you at the bottom of this post.
Example of usage:
>> A=ZeroBased(1:5)
A =
1 2 3 4 5
>> A(0:2)=A(0:2)*10
A =
10 20 30 4 5
%%%%%%%Code below must go in a file called ZeroBased.m
classdef ZeroBased < double
%ZeroBased - a class essentially the same as @double, but can be indexed
%starting from 0.
methods
function obj=ZeroBased(data)
%constructor for ZeroBased
obj=obj@double(data);
end
function objnew=subsref(obj,S)
%SUBSREF for ZeroBased class
nn=length(S.subs);
for ii=1:nn
if ~(ischar(S.subs{ii})|islogical(S.subs{ii})),
S.subs{ii}=S.subs{ii}+1;
end
end
objnew=subsref@double(obj,S);
end
function objnew=subsasgn(obj,S,rhs)
%SUBSASGN for ZeroBased class
nn=length(S.subs);
for ii=1:nn
if ~(ischar(S.subs{ii})|islogical(S.subs{ii})),
S.subs{ii}=S.subs{ii}+1;
end
end
objnew=subsasgn@double(obj,S,rhs);
end
function display(obj)
l=inputname(1);
if isempty(l), l='ans'; end
disp ' '
disp([l ' = ']);
disp ' '
disp(double(obj));
end
function obj=uplus(obj)
end
function obj=uminus(obj)
obj=ZeroBased(-double(obj));
end
function obj=plus(L,R)
obj = ZeroBased( double(L)+double(R) );
end
function obj=minus(L,R)
obj = ZeroBased( double(L)-double(R) );
end
function obj=times(L,R)
obj = ZeroBased( double(L).*double(R) );
end
function obj=mtimes(L,R)
obj = ZeroBased( double(L)*double(R) );
end
function obj=rdivide(L,R)
obj = ZeroBased( double(L)./double(R) );
end
function obj=ldivide(L,R)
obj = ZeroBased( double(L).\double(R) );
end
function obj=mrdivide(L,R)
obj = ZeroBased( double(L)/double(R) );
end
function obj=mldivide(L,R)
obj = ZeroBased( double(L)\double(R) );
end
end
end
|