How to get indexing to start at 0?

7 ビュー (過去 30 日間)
Kyle
Kyle 2012 年 6 月 1 日
I'm having trouble getting matlab to index at zero. I need nn and mm to start at zero but i get a error for subscripts. I know matlab indexing starts at 1 but im required to start it at 0 for this assignment.
This code may not be executable since the spfirst file might be required. Any ideas?
%Initialize inputs
xx=[ones(1,10),zeros(1,5)];
bb=1/4*ones(1,4);
%Create Filtered output
yy=firfilt(bb,xx);
%Determine length
Lengthyy=length(xx)+length(bb)-1;
fprintf('The Length of yy will be %d\n',Lengthyy)
fprintf('%d',yy(1))
nn = 1:length(xx); %--- use first=1 and last=length(xx)
mm = 1:length(yy);
subplot(2,1,1);
stem(nn,xx(nn))
subplot(2,1,2);
stem(mm,yy(mm),'filled') %--Make black dots
xlabel('Time Index (n)')
  1 件のコメント
Honglei Chen
Honglei Chen 2012 年 6 月 1 日
Could you elaborate more what your question is? You can simply minus 1 to go from 1 based to 0 based unless you have other requirements.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 6 月 1 日
In order to get MATLAB to index starting from 0, you need to create an extensive set of Object Oriented Programming classes that behave like normal numeric classes except that they differ in their implementation of subsref() and subsassgn() and sub2ind() and ind2sub().
I seem to recall that someone has done a fair bit of work on writing a class of objects that behave like numbers by default but can be extended by the users, but unfortunately I do not remember whom and I do not seem to find the relevant MATLAB File Exchange contribution at this time. If you are not able to find it, you should expect it to take you months of work to do the implementation.
Please consider the possibility that your assignment might require that you start m and n from 0, but not require that you index from 0.
  1 件のコメント
Kyle
Kyle 2012 年 6 月 3 日
Thanks Walter and Honglei for the help, this is what i needed to do:
subplot(2,1,1);
stem(nn-1,xx(nn))
subplot(2,1,2);
stem(mm-1,yy(mm),'filled') %--Make black dots

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by