How to get indexing to start at 0?

7 Ansichten (letzte 30 Tage)
Kyle
Kyle am 1 Jun. 2012
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 Kommentar
Honglei Chen
Honglei Chen am 1 Jun. 2012
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 1 Jun. 2012
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 Kommentar
Kyle
Kyle am 3 Jun. 2012
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by