Mathematica Mathematica Circular Shift of Array Elements

AI Thread Summary
A Mathematica user seeks an efficient way to circularly shift array elements without iteration, specifically for large datasets. They discover the functions RotateLeft and RotateRight, which work well for individual elements. However, when attempting to simplify their loop, they encounter a recursion limit error due to the size of their data (35,000 elements). The user realizes that the issue arises from matrix multiplication involving incompatible shapes, which is resolved by transposing their column vector. They also identify that their array is being treated as a 2D array, complicating further operations. To convert their array into a vector, they use the Flatten function. Lastly, they face challenges in constructing a 2D array for plotting but eventually resolve the issue by correcting the length of their time vector.
rynlee
Messages
44
Reaction score
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
 
Physics news on Phys.org
There are two functions RotateRight and RotateLeft
 
that should work perfectly, thanks
 
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
 
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
 
OK, I tried it without the line:

correlationFunction[[a]] = delayDeltaOmega.deltaOmega;

and it worked fine, so my problem must be with the matrix multiplication
 
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. >>
 
AHA!

I thought mathematica automatically transposed for dot products, it does not, I needed to transpose my column vector. problem solved for now
 
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
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
rynlee said:
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
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
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
Nevermind, the length of the time vector was too long, I fixed it and things seem to work now.
 

Similar threads

Back
Top