Mathematica Circular Shift of Array Elements

Click For Summary
SUMMARY

The forum discussion centers on efficiently performing a circular shift of array elements in Mathematica using the functions RotateLeft and RotateRight. The user, Ryn, initially faced performance issues due to iterating through a large dataset (35,000 elements). After implementing vector operations, Ryn encountered errors related to matrix dimensions and multiplication, specifically with the Dot function. The solution involved transposing the column vector and using the Flatten function to convert arrays into vectors, ultimately resolving the dimensionality issues for plotting.

PREREQUISITES
  • Understanding of Mathematica functions, specifically RotateLeft and RotateRight
  • Familiarity with matrix operations and the Dot product in Mathematica
  • Knowledge of array dimensions and manipulation in Mathematica
  • Experience with data visualization in Mathematica, particularly using ListPlot
NEXT STEPS
  • Explore advanced array manipulation techniques in Mathematica
  • Learn about performance optimization strategies for large datasets in Mathematica
  • Investigate the use of Transpose and Flatten for array transformations
  • Study the intricacies of the Dot product and its requirements for compatible dimensions
USEFUL FOR

Data scientists, Mathematica users, and anyone working with large datasets who needs to optimize array operations and visualizations.

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

  • · Replies 4 ·
Replies
4
Views
869
  • · Replies 1 ·
Replies
1
Views
7K
Replies
43
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K