My colleague asked me is there any function to reverse an array. She needs to reverse an array of 32 elements, which means that 1st element become last element, 2nd element become 2nd last element and so on:
A[0] ---> A[31]
A[1] ---> A[30]
.....
.....
.....
A[30] ---> A[1]
A[31] ---> A[0]
I immediately thought of 'slicing array' operator (A[a:b]) may be used in this case. Let say we have an array A with size of 32. I'm thinking of using:
Reversed_array = A[31:0]
However, VEE shows Run Time Error when running the code as slice array syntax A[a:b] requires a <= b. A quick solution for this is to write our own function. However, I'm lazy to use For Loop to reverse the array as I'm lazy to create so many containers blocks. Also, we can use stack, but that cannot get rids of the For Loop also. My solution is making use of array functions like Sum() and Ramp() and also Formula container's dynamic formula. This will eleminate First, we'll be using the Sum() and Ramp() to generate text like "[ A[31], A[30]....]". Then the text generated will be passed to the Formula container as input parameter that will be evaluated and function return value. Figure 1 Shows the Reverse Array function
Figure 1: Reverse Array function
Figure 2 Shows the Result
Figure 2: Result
Let me know if there is any other better approach.
Download the source file here.
0 comments:
Post a Comment