Thread Subject: simple average magitude diff

Subject: simple average magitude diff

From: astro mmi

Date: 21 Nov, 2009 23:50:04

Message: 1 of 4

Hello everyone,
  How can I implement the following segment without getting the error:
"Subscript indices must either be real positive integers or logicals."
for k=0:N-1
     for n=0:N-1
         amdf(k)=(1/N)*sum(y(n)-y(n-k));
        figure(1)
         plot(amdf)
     end
 end
Thanx.

Subject: simple average magitude diff

From: Matt

Date: 22 Nov, 2009 00:10:25

Message: 2 of 4

"astro mmi" <pyarsa_madhu@yahoo.co.in> wrote in message <he9ubc$q8s$1@fred.mathworks.com>...
> Hello everyone,
> How can I implement the following segment without getting the error:
> "Subscript indices must either be real positive integers or logicals."
> for k=0:N-1
> for n=0:N-1
> amdf(k)=(1/N)*sum(y(n)-y(n-k));
> figure(1)
> plot(amdf)
> end
> end
> Thanx.

for example:

for k=1:N
     for n=1:N
       try
         amdf(k)=(1/N)*sum(y(n)-y(n-k));
     catch
     disp 'Index n-k is not between 1 and length(y). Skipping...'
     end



        figure(1)
         plot(amdf)
     end
 end

Subject: simple average magitude diff

From: Royi Avital

Date: 22 Nov, 2009 00:32:05

Message: 3 of 4

"astro mmi" <pyarsa_madhu@yahoo.co.in> wrote in message <he9ubc$q8s$1@fred.mathworks.com>...
> Hello everyone,
> How can I implement the following segment without getting the error:
> "Subscript indices must either be real positive integers or logicals."
> for k=0:N-1
> for n=0:N-1
> amdf(k)=(1/N)*sum(y(n)-y(n-k));
> figure(1)
> plot(amdf)
> end
> end
> Thanx.

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.

Subject: simple average magitude diff

From: Matt

Date: 22 Nov, 2009 14:04:06

Message: 4 of 4

"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
index from 0 Matt J 23 Nov, 2009 13:41:13
oop Matt J 23 Nov, 2009 13:40:34
subscript indic... astro mmi 21 Nov, 2009 18:54:03
rssFeed for this Thread

Contact us at files@mathworks.com