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:
Looping within elements of a vector

Subject: Looping within elements of a vector

From: Simon Croft

Date: 26 Feb, 2009 14:52:02

Message: 1 of 11

Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.

I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.

So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).

Any help much appreciated.

Regards,
Simon

Subject: Looping within elements of a vector

From: Eric Williams

Date: 26 Feb, 2009 15:03:01

Message: 2 of 11

"Simon Croft" <sac125@york.ac.uk> wrote in message <go6aai$g4q$1@fred.mathworks.com>...
> Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.
>
> I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.
>
> So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).
>
> Any help much appreciated.
>
> Regards,
> Simon

A = 1:7
ind = 10
A((mod(ind,length(A))+(ind==length(A)) = 9999
disp(A)

Subject: Looping within elements of a vector

From: John D'Errico

Date: 26 Feb, 2009 15:05:04

Message: 3 of 11

"Simon Croft" <sac125@york.ac.uk> wrote in message <go6aai$g4q$1@fred.mathworks.com>...
> Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.
>
> I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.
>
> So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).
>
> Any help much appreciated.

help circshift

john

Subject: Looping within elements of a vector

From: Eric Williams

Date: 26 Feb, 2009 15:09:02

Message: 4 of 11

"Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6av5$i5$1@fred.mathworks.com>...
> "Simon Croft" <sac125@york.ac.uk> wrote in message <go6aai$g4q$1@fred.mathworks.com>...
> > Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.
> >
> > I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.
> >
> > So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).
> >
> > Any help much appreciated.
> >
> > Regards,
> > Simon
>
> A = 1:7
> ind = 10
> A((mod(ind,length(A))+(ind==length(A)) = 9999
> disp(A)

So much for not proof-reading/testing my code, sorry about that. The actual code is:

A = 1:7
ind = [10 7]
A(mod(ind,length(A))+((ind==length(A))*length(A))) = 9999
disp(A)

Subject: Looping within elements of a vector

From: Eric Williams

Date: 26 Feb, 2009 15:18:02

Message: 5 of 11

"Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6av5$i5$1@fred.mathworks.com>...
> "Simon Croft" <sac125@york.ac.uk> wrote in message <go6aai$g4q$1@fred.mathworks.com>...
> > Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.
> >
> > I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.
> >
> > So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).
> >
> > Any help much appreciated.
> >
> > Regards,
> > Simon
>

I believe the OP was looking for a way to access the elements of the vector in this manner, not change the actual vector. Therefor circshift would be of little use.

Subject: Looping within elements of a vector

From: Simon Croft

Date: 26 Feb, 2009 15:35:03

Message: 6 of 11

"Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6bra$3b9$1@fred.mathworks.com>...
> "Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6av5$i5$1@fred.mathworks.com>...
> > "Simon Croft" <sac125@york.ac.uk> wrote in message <go6aai$g4q$1@fred.mathworks.com>...
> > > Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.
> > >
> > > I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.
> > >
> > > So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).
> > >
> > > Any help much appreciated.
> > >
> > > Regards,
> > > Simon
> >
>
> I believe the OP was looking for a way to access the elements of the vector in this manner, not change the actual vector. Therefor circshift would be of little use.

Yeah, circshift doesn't help, but thanks for the suggestion, John.

Eric, thank you for your answer, but I must confess to not really understanding what your code is doing other than turning the 3rd and 7th entries into the value 9999.

For the example of A=1:7, I would like to be able to say A(i)+A(i+5) for 7>=i>=1. I can get this to work as long as i isn't 2 by simply putting A(i)+A(mod(i+5,7)). This works fine until you put i=2, which returns A(2)+A(0) instead of A(2)+A(7).

Sorry if your code is the solution and I'm just not understanding it.

Thanks for your help,
Simon

Subject: Looping within elements of a vector

From: Eric Williams

Date: 26 Feb, 2009 15:39:01

Message: 7 of 11

"Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6bae$pek$1@fred.mathworks.com>...
> "Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6av5$i5$1@fred.mathworks.com>...
> > "Simon Croft" <sac125@york.ac.uk> wrote in message <go6aai$g4q$1@fred.mathworks.com>...
> > > Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.
> > >
> > > I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.
> > >
> > > So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).
> > >
> > > Any help much appreciated.
> > >
> > > Regards,
> > > Simon
> >
> > A = 1:7
> > ind = 10
> > A((mod(ind,length(A))+(ind==length(A)) = 9999
> > disp(A)
>
> So much for not proof-reading/testing my code, sorry about that. The actual code is:
>
> A = 1:7
> ind = [10 7]
> A(mod(ind,length(A))+((ind==length(A))*length(A))) = 9999
> disp(A)

I probably did more damage than good here:

A = 1:7
ind = [10 7 14 1]
A(mod(ind,length(A))+(mod(ind,length(A))==0)*length(A)) = 9999
disp(A)

Subject: Looping within elements of a vector

From: Eric Williams

Date: 26 Feb, 2009 16:36:01

Message: 8 of 11

"Simon Croft" <sac125@york.ac.uk> wrote in message <go6cr7$bim$1@fred.mathworks.com>...
> "Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6bra$3b9$1@fred.mathworks.com>...
> > "Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6av5$i5$1@fred.mathworks.com>...
> > > "Simon Croft" <sac125@york.ac.uk> wrote in message <go6aai$g4q$1@fred.mathworks.com>...
> > > > Hi there. I'm sure there must be a pretty simple solution to this, but thus far it has eluded me.
> > > >
> > > > I want to be able to move forwards and backwards a certain number of elements within a vector, and when I get to the beginning/end return to the other end.
> > > >
> > > > So for example, if I have a vector A with n elements, I effectively want to achieve A(n+4)=A(4). I found a way using the modulo function which works for the most part, but problems arise when you fall on the n'th element as this returns A(0) instead of A(n).
> > > >
> > > > Any help much appreciated.


> > > >
> > > > Regards,
> > > > Simon
> > >
> >
> > I believe the OP was looking for a way to access the elements of the vector in this manner, not change the actual vector. Therefor circshift would be of little use.
>
> Yeah, circshift doesn't help, but thanks for the suggestion, John.
>
> Eric, thank you for your answer, but I must confess to not really understanding what your code is doing other than turning the 3rd and 7th entries into the value 9999.
>
> For the example of A=1:7, I would like to be able to say A(i)+A(i+5) for 7>=i>=1. I can get this to work as long as i isn't 2 by simply putting A(i)+A(mod(i+5,7)). This works fine until you put i=2, which returns A(2)+A(0) instead of A(2)+A(7).
>
> Sorry if your code is the solution and I'm just not understanding it.
>
> Thanks for your help,
> Simon

I may have misunderstood your intentions, but what I gave you was a way to access the "i"th element in a vector of length N, where A(N + i) = A(i).

For the example you gave, instead of saying A(i)+A(i+5), instead replace your address with:

A(mod(i,length(A))+(mod(i,length(A))==0)*length(A)) + A(mod((i+b),length(A))+(mod((i+b),length(A))==0)*length(A))

A = 1:7
i = 4
b = 5
z = A(mod(i,length(A))+(mod(i,length(A))==0)*length(A)) + A(mod((i+b),length(A))+(mod((i+b),length(A))==0)*length(A))

or to use your with hard coded numbers

A(i)+A(mod(i+5,7)+((mod((i+5),7)==0)*7))

The key to getting rid of your zero problem is to add the logical value of the check if mod(i+5,7) is 0, and if it is, multiply that logical value (1) by 7, to bring you to the end.

Mine is just generalized so you can use any vector (A), any index (i), and any addition (b) without having to know that A has 7 elements or that b is 5.

On the other hand, if what you're doing doesn't require that, it may be easier to use the second solution for code readability.

Finally, if what you are doing is exactly what your example is (adding elements that are spaced a certain distance after looping), then maybe circshift could help:

To add any element and the element 5 away from it to the right:

A = 1:7
shift = 5
b = circshift(A,[0 length(A)-shift])
c = A+b

Let me know if you misunderstood again.

Subject: Looping within elements of a vector

From: Eric Williams

Date: 26 Feb, 2009 16:44:02

Message: 9 of 11

P.S. That was supposed to read: "Please let me know if I misunderstood you again"... talk about a bad typo day.

Subject: Looping within elements of a vector

From: Simon Croft

Date: 26 Feb, 2009 17:20:17

Message: 10 of 11

"Eric Williams" <gmail@ewilliams2006.com> wrote in message <go6gdh$lbn$1@fred.mathworks.com>...

> I may have misunderstood your intentions, but what I gave you was a way to access the "i"th element in a vector of length N, where A(N + i) = A(i).
>
> For the example you gave, instead of saying A(i)+A(i+5), instead replace your address with:
>
> A(mod(i,length(A))+(mod(i,length(A))==0)*length(A)) + A(mod((i+b),length(A))+(mod((i+b),length(A))==0)*length(A))
>
> A = 1:7
> i = 4
> b = 5
> z = A(mod(i,length(A))+(mod(i,length(A))==0)*length(A)) + A(mod((i+b),length(A))+(mod((i+b),length(A))==0)*length(A))
>
> or to use your with hard coded numbers
>
> A(i)+A(mod(i+5,7)+((mod((i+5),7)==0)*7))
>
> The key to getting rid of your zero problem is to add the logical value of the check if mod(i+5,7) is 0, and if it is, multiply that logical value (1) by 7, to bring you to the end.
>
> Mine is just generalized so you can use any vector (A), any index (i), and any addition (b) without having to know that A has 7 elements or that b is 5.
>
> On the other hand, if what you're doing doesn't require that, it may be easier to use the second solution for code readability.
>
> Finally, if what you are doing is exactly what your example is (adding elements that are spaced a certain distance after looping), then maybe circshift could help:
>
> To add any element and the element 5 away from it to the right:
>
> A = 1:7
> shift = 5
> b = circshift(A,[0 length(A)-shift])
> c = A+b
>
> Let me know if you misunderstood again.

Thanks for your help Eric, I'll have a play with what you've suggested when I get a chance and get back to you.

Thanks again,
Simon

Subject: Looping within elements of a vector

From: Simon Croft

Date: 26 Feb, 2009 17:40:18

Message: 11 of 11

Ok, I've taken the time to read through what you've written, and that solves the problem nicely.

Thanks very much for your time and help, it's much appreciated. I can feel my headache lifting :-D

Tags for this Thread

No tags are associated with 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