Mathematica Circular Shift of Array Elements

In summary, the mathematician is trying to do a circular shift of an array of numbers, and is having trouble due to the large size of the numbers and the fact that they are vectors. There are two functions that should work perfectly, but when he tries to simplify his loop he gets thrown tons of errors. He is also having trouble with plotting values because he is trying to create a dimension {2} array for a ListPlot. Once he fixes the length of the time vector, things seem to work fine.
  • #1
rynlee
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
 
Physics news on Phys.org
  • #2
There are two functions RotateRight and RotateLeft
 
  • #3
that should work perfectly, thanks
 
  • #4
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
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
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
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
AHA!

I thought mathematica automatically transposed for dot products, it does not, I needed to transpose my column vector. problem solved for now
 
  • #9
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.
 

1. What is a circular shift of array elements in Mathematica?

A circular shift of array elements in Mathematica is a method of rearranging the elements in an array by shifting them to the left or right in a circular fashion. This means that the elements at the beginning or end of the array will wrap around to the opposite end.

2. How do I perform a circular shift of array elements in Mathematica?

To perform a circular shift of array elements in Mathematica, you can use the built-in function RotateLeft or RotateRight. These functions take an array as the first argument and the number of positions to shift as the second argument. For example, RotateLeft[{1,2,3,4},2] will shift the elements of the array two positions to the left.

3. Can I perform a circular shift on multi-dimensional arrays in Mathematica?

Yes, you can perform a circular shift on multi-dimensional arrays in Mathematica. The RotateLeft and RotateRight functions can be used on arrays with any number of dimensions.

4. What is the purpose of a circular shift in Mathematica?

A circular shift in Mathematica can be useful for a variety of purposes, such as creating a sliding window over an array, shifting elements to align with other arrays, or implementing circular convolution in signal processing.

5. Are there any other functions in Mathematica that can achieve a circular shift?

Yes, in addition to RotateLeft and RotateRight, there are other functions in Mathematica that can achieve a circular shift, such as CircularShift and ArrayPad. These functions offer more options for specifying the direction and amount of shift.

Similar threads

  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • Programming and Computer Science
Replies
17
Views
2K
Replies
11
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Computing and Technology
Replies
2
Views
722
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
30
Views
6K
Back
Top