Mathematica Circular Shift of Array Elements

  • Mathematica
  • Thread starter rynlee
  • Start date
  • #1
45
0
Hi All,

does anyone know of a mathematica function that efficiently circularly shifts all array elements?

e.g.
{1,2,3,4,5} -> {4,5,1,2,3}

without iterating? The reason I ask is because I have a huge data set I'm working with and right now iterating through is very processing intensive and is taking forever. I figured out a way to do what I need to do however via vector operations without the need to iterate, which I suspect may be a more efficient process in the mathematica back end, but to do so I need to perform this circular transformation first, and iterating through each element would defeat the purpose.

Thanks,
ryn
 

Answers and Replies

  • #2
29,951
6,342
There are two functions RotateRight and RotateLeft
 
  • #3
45
0
that should work perfectly, thanks
 
  • #4
45
0
So, that appears to be working (when I test individual matrix elements, the functions RotateLeft and RotateRight work perfectly).

Unfortunately, when I try to simplify my loop, I get thrown tons of errors like these:
$RecursionLimit::reclim:Recursion Depth of 256 exceeded

which I didn't get before. My loop is now this:

Code:
Do[
 delayDeltaOmega = RotateLeft[deltaOmega, a - 1]
    correlationFunction[[a]] = delayDeltaOmega.deltaOmega
 , {a, 1, length/5, 1}]
the actual variable length/5 doesn't matter (it's an integer, and I checked it works earlier). Is something wrong elsewhere?

Thanks for taking a look,

Ryn
 
  • #5
45
0
I should say that the issue may be that each vector has 35000 elements. That's certainly why it was taking so long before. The 'length' by the way is 35000 and deltaOmega is just a 35000x1 array.

Thanks,
Ryn
 
  • #6
45
0
OK, I tried it without the line:

correlationFunction[[a]] = delayDeltaOmega.deltaOmega;

and it worked fine, so my problem must be with the matrix multiplication
 
  • #7
45
0
delayDeltaOmega.deltaOmega gives this error:

Dot::dotsh: Tensors {{-1.37},{-2.37},{-2.37},{-2.37},{-3.37},{-1.37},<<39>>,{-8.37},{-5.37},{-7.37},{-6.37},{-6.37},<<65486>>} and {{10.6},{9.63},{8.63},{6.63},{3.63},{3.63},<<39>>,{5.63},{6.63},{6.63},{6.63},{6.63},<<65486>>} have incompatible shapes. >>
 
  • #8
45
0
AHA!

I thought mathematica automatically transposed for dot products, it does not, I needed to transpose my column vector. problem solved for now
 
  • #9
29,951
6,342
Mathematica does not automatically transpose if you make your array a 2D array, but if you simply use 1D vectors then you don't have to transpose.
 
  • #10
45
0
Hm, it seems then that I'm dealing with a 2D array with only one column. When I call

Dimensions[deltaOmega] prior to the loop, I get

{big number, 1}

This seems to then be causing me trouble later, because after I do the multiplication (it is much faster now btw), I get dimensions of the resulting correlationFunction

Dimensions[correlationFunction]
{big number, 1, 1}

This is problematic, as it makes it hard for me to plot it.

Is there a way I can turn my array (which came from the data I imported for some reason it was in that form) into a vector? Would that solve my problems?

Vectors aside, multiplying a dimensions {x,1} array by a dimensions Transpose[{x,1}] array should give me just a scalar shouldn't it? Is something odd happening with my dimensions in

Code:
Do[
 delayDeltaOmega = RotateLeft[deltaOmega, a - 1]
    correlationFunction[[a]] = delayDeltaOmega.deltaOmega
 , {a, 1, length/5, 1}]
?

When I define correlationFunction I give it as:

Code:
correlationFunction = Array[0 &, {length/5, 1}];
 
  • #11
29,951
6,342
Is there a way I can turn my array (which came from the data I imported for some reason it was in that form) into a vector?
Just use Flatten
 
Last edited:
  • #12
45
0
Thanks, that works nicely.

Now I'm running into a different issue, for ListPlot I'm trying construct a dimension {big number,2} array to give to ListPlot with one column the x-axis numbers and the other column the y-axis numbers, but I'm having trouble doing that with things being vectors. I've tried:

{time, correlationFunction}, but for some reason that gives dimensions {2}
 
  • #13
45
0
I tried a number of things, even something like this:

Code:
dataPlot = Array[0 &, {length/5, 2}];
dataPlot[[All, 1]] = time;
dataPlot[[All, 2]] = correlationFunction;
which gives me dimensions {big number, 2}, but when I try to look at values, for example row 20,

Code:
dataPlot[[20]]
I don't get a pair of numbers, I get all the numbers somehow.

Also

Code:
Transpose[{time, correlationFunction}]
also does not work, it gives me dimensions {1}
 
Last edited:
  • #14
45
0
Nevermind, the length of the time vector was too long, I fixed it and things seem to work now.
 

Related Threads on Mathematica Circular Shift of Array Elements

Replies
4
Views
7K
Replies
2
Views
2K
Replies
1
Views
3K
Replies
5
Views
5K
Replies
3
Views
4K
Replies
1
Views
496
Replies
5
Views
23K
Replies
10
Views
2K
Top