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:
shifting vectors according to an input integer

Subject: shifting vectors according to an input integer

From: Raymond

Date: 10 Sep, 2010 03:20:11

Message: 1 of 5

I'm trying to write this function that will shift whatever input vector the amount of times specified. So there are two inputs and one output.
An example:
input 1: [1 2 3 4 5]
input 2: 4 (could be a negative integer as well)
So for the above case, the function would operate like this:
[1 2 3 4 5]
[5 1 2 3 4]
[4 5 1 2 3]
[3 4 5 1 2]
[2 3 4 5 1]
It has shifted 4 (input 2) times and the desired answer is [2 3 4 5 1].
If the number were negative:
input 1: [1 2 3 4 5]
input 2: -2

[1 2 3 4 5]
[2 3 4 5 1]
[3 4 5 1 2]
It would go backwards. This function has to work for columns as well, however they are all single rows or single columns.
My work so far:
 B=1:length(vec); %vec is input 1
mod(shift, length(vec)) %shift is input 2
So it has something to do with the mod function...because that's what we're practicing with. Now I'm pretty much stumped how to proceed?

Thank you

Subject: shifting vectors according to an input integer

From: Matt Fig

Date: 10 Sep, 2010 04:00:21

Message: 2 of 5

Have you seen CIRCSHIFT?

Subject: shifting vectors according to an input integer

From: Raymond

Date: 10 Sep, 2010 04:10:22

Message: 3 of 5

"Matt Fig" <spamanon@yahoo.com> wrote in message <i6cagl$7ns$1@fred.mathworks.com>...
> Have you seen CIRCSHIFT?

Not allowed to use that unfortunately. Only using mod, :, vec(vec())...etc.

Subject: shifting vectors according to an input integer

From: Ross W

Date: 10 Sep, 2010 07:24:04

Message: 4 of 5

"Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i6cb3e$dmo$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <i6cagl$7ns$1@fred.mathworks.com>...
> > Have you seen CIRCSHIFT?
>
> Not allowed to use that unfortunately. Only using mod, :, vec(vec())...etc.

Hi

first guess:
B=1:numel(vec)
e.g. B=[1 2 3 4 5]

take your B, add the SHIFT
suppose SHIFT=2
C=B+SHIFT
e.g. C=[3 4 5 6 7]

now the 3 4 5 are fine, but the 6 7 are wrong, they should be 1 2
so try using mod here
C=mod(B+SHIFT,numel(vec));

I know this isn't right (and yes I do have a solution), but maybe you can figure out what to do next?

Ross

Subject: shifting vectors according to an input integer

From: Sean

Date: 10 Sep, 2010 13:27:23

Message: 5 of 5

"Raymond " <atlantaswagsurfers3@yahoo.com> wrote in message <i6cb3e$dmo$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <i6cagl$7ns$1@fred.mathworks.com>...
> > Have you seen CIRCSHIFT?
>
> Not allowed to use that unfortunately. Only using mod, :, vec(vec())...etc.

Like this?
%Vector to shift and shift
A = [1 2 3 4 5];
shift = 3;

%Matrix of combos (twice the size for the purpose of negative shifts)
T = repmat(triu(toeplitz(A))+tril(toeplitz([0 A(end:-1:2)])),2,1);

%Row Selection
T(max(rem(shift,5)+1,numel(A)+abs(mod(shift,5))+1),:)

:)

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