Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to create a circular vector / variable

Subject: How to create a circular vector / variable

From: abcdefg k

Date: 19 Oct, 2010 14:04:04

Message: 1 of 7

Hello,
I have a vector of length 'n=100'. For each index, I have to calculate sum of next 10 consecutive numbers. When it comes to the end of vector it should calculate sum of first 10 numbers in a cyclic manner. Is there any method to create a vector in a cyclic manner

Ex: a=1:100;
for i=1:91
b(i)=a(i)+......+a(i+9);
end


b(92)=a(92)+a(93)+....+a(100)+a(1);
b(93)=a(93)+a(94)+.....+a(100)+a(1)+a(2);
b(94)=a(94)+a(95)+......+a(100)+a(1)+a(2);

b(100)=a(100)+a(1)+a(2)...+a(9);

Is there any better way to write this code.

Subject: How to create a circular vector / variable

From: Sean

Date: 19 Oct, 2010 14:23:03

Message: 2 of 7

"abcdefg k" <jagadishkota.iitr@gmail.com> wrote in message <i9k8gk$g4f$1@fred.mathworks.com>...
> Hello,
> I have a vector of length 'n=100'. For each index, I have to calculate sum of next 10 consecutive numbers. When it comes to the end of vector it should calculate sum of first 10 numbers in a cyclic manner. Is there any method to create a vector in a cyclic manner
>
> Ex: a=1:100;
> for i=1:91
> b(i)=a(i)+......+a(i+9);
> end
>
>
> b(92)=a(92)+a(93)+....+a(100)+a(1);
> b(93)=a(93)+a(94)+.....+a(100)+a(1)+a(2);
> b(94)=a(94)+a(95)+......+a(100)+a(1)+a(2);
>
> b(100)=a(100)+a(1)+a(2)...+a(9);
>
> Is there any better way to write this code.

You need to learn to use indexing:

b(i) = sum(a(i:i+9));

The easiest way to take care of the cyclic issue is to just make 'a' twice as long:
a = repmat(a,1,2);

and then run your for loop all of the way to 100.

Subject: How to create a circular vector / variable

From: Matt J

Date: 19 Oct, 2010 14:39:03

Message: 3 of 7

"abcdefg k" <jagadishkota.iitr@gmail.com> wrote in message <i9k8gk$g4f$1@fred.mathworks.com>...
> Hello,
> I have a vector of length 'n=100'. For each index, I have to calculate sum of next 10 consecutive numbers. When it comes to the end of vector it should calculate sum of first 10 numbers in a cyclic manner. Is there any method to create a vector in a cyclic manner
>
> Ex: a=1:100;
> for i=1:91
> b(i)=a(i)+......+a(i+9);
> end
=====

You can perform the sum by matrix multiplication with a sparse, circulant matrix. You can create such a matrix using this tool

http://www.mathworks.com/matlabcentral/fileexchange/26292-regular-control-point-interpolation-matrix-with-boundary-conditions

In your case, this would be

A=interpMatrix(ones(1,10),1,n,1,'circ'); %Sparse circulant summation matrix

b=a(:).'*A;

Subject: How to create a circular vector / variable

From: Matt J

Date: 19 Oct, 2010 14:43:04

Message: 4 of 7

"abcdefg k" <jagadishkota.iitr@gmail.com> wrote in message <i9k8gk$g4f$1@fred.mathworks.com>...
> Hello,
> I have a vector of length 'n=100'. For each index, I have to calculate sum of next 10 consecutive numbers. When it comes to the end of vector it should calculate sum of first 10 numbers in a cyclic manner. Is there any method to create a vector in a cyclic manner
>
> Ex: a=1:100;
> for i=1:91
> b(i)=a(i)+......+a(i+9);
> end
======

Recognizing that 'b" is just a circulant convolution of "a" with ones(1,10), you could also implement this using FFTs.

In this case, however, it's probably better to do it in the non-Fourier domain, using the matrix method I proposed, since it is then a more sparse computation.

Subject: How to create a circular vector / variable

From: Matt J

Date: 19 Oct, 2010 15:09:04

Message: 5 of 7


Yet another way

b=conv([a,a(1:9)],ones(1,10),'valid');

Subject: How to create a circular vector / variable

From: Steven_Lord

Date: 19 Oct, 2010 16:43:24

Message: 6 of 7



"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:i9kai7$5tj$1@fred.mathworks.com...
> "abcdefg k" <jagadishkota.iitr@gmail.com> wrote in message
> <i9k8gk$g4f$1@fred.mathworks.com>...
>> Hello,
>> I have a vector of length 'n=100'. For each index, I have to calculate
>> sum of next 10 consecutive numbers. When it comes to the end of vector it
>> should calculate sum of first 10 numbers in a cyclic manner. Is there
>> any method to create a vector in a cyclic manner
>>
>> Ex: a=1:100;
>> for i=1:91
>> b(i)=a(i)+......+a(i+9);
>> end
> =====
>
> You can perform the sum by matrix multiplication with a sparse, circulant
> matrix. You can create such a matrix using this tool
>
> http://www.mathworks.com/matlabcentral/fileexchange/26292-regular-control-point-interpolation-matrix-with-boundary-conditions
>
> In your case, this would be
>
> A=interpMatrix(ones(1,10),1,n,1,'circ'); %Sparse circulant summation
> matrix
>
> b=a(:).'*A;

Does this do the same thing as GALLERY with the 'circul' option?

A = gallery('circul', sparse([ones(1, 10) zeros(1, 90)]));

http://www.mathworks.com/help/techdoc/ref/gallery.html#f84-999880

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to create a circular vector / variable

From: Matt J

Date: 19 Oct, 2010 17:46:03

Message: 7 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <i9khrc$93q$1@fred.mathworks.com>...
>> > A=interpMatrix(ones(1,10),1,n,1,'circ'); %Sparse circulant summation
> > matrix
> >
> > b=a(:).'*A;
>
> Does this do the same thing as GALLERY with the 'circul' option?
>
> A = gallery('circul', sparse([ones(1, 10) zeros(1, 90)]));
>
> http://www.mathworks.com/help/techdoc/ref/gallery.html#f84-999880
==============

It appears to give the transpose of what my routine gives, Steve.
However, observe from the tests below that

(1) My routine is considerably faster, even if I bear the overhead of the transposition.

(2) GALLERY appears to have a glitch (in R2009b) in that it doesn't produce a valid sparse matrix. In particular, it appears not to weed out explicit zeros, so that even if the two matrices are equal in the eyes of ISEQUAL, the one produced by GALLERY consumes considerably more memory (according to whos()).

m=10;
n=1000;

tic;
A1=interpMatrix(ones(1,m),1,n,1,'circ').';
toc;
%Elapsed time is 0.002381 seconds.


tic;
A2 = gallery('circul', sparse([ones(1, m) zeros(1, n-m)]));
toc;
%Elapsed time is 0.150288 seconds.

EqualIfOne = isequal(A1,A2), %=1

whos A1 A2


%{

  Name Size Bytes Class Attributes

  A1 1000x1000 124004 double sparse
  A2 1000x1000 11980244 double sparse

%}

Tags for this Thread

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.

Contact us