How to get indexing to start at 0?
조회 수: 6 (최근 30일)
이전 댓글 표시
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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!