LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to reverse 2d array

Solved!
Go to solution

hi

          i have 2d array with this format .

           

 

         1 2 3

         4 5 6

         7 8 9

i want to reverce thhis 

 

         3 2 1

         6 5 4

         9 8 7

without using any loop it is possible.using labview8.2

0 Kudos
Message 1 of 10
(12,632 Views)
Solution
Accepted by topic author GokulGKM

Let's say LabVIEW has a built in function to do that for you.  The underlying logic will use a loop.  Why?  Because it's a rather strange restriction to put into place to disallow loops when you want to perform an option that requires several nearly identical iterations.

 

Why are you trying to avoid using loops?

 

Let's take a look at the code that can do what you ask.  Then, let's take a better look at it and why we want to come back to the bolded question?

 

sillycode.png

 

This will take your array, split off a single row at a time, use the Reverse 1D Array function (which is going to run a loop internally) on each row, build those rows back into the 2D array, and output the result. 

 

Now, what happens if you want to add a fourth row to this 2D array?  You have to drop the same logic down again, wire in the correct row, extend the build array, etc.  This might be something you can manage.  But, what if you want to expand it to an array with 100 rows?  Now, the work gets to be quite a bit more and the code becomes essentially impossible to read.  You're not getting any value here by avoiding the loop.

 

Let's take it one step further.  What if you want to work with arrays of variable sizes?  Now, what are you going to do?  Your code is designed to work with one size of array (and rather clunky in doing so). 

 

So, we come back to the same question we've had this entire time.  Why, exactly, are you trying to avoid loops?  Doing this without a loop is poor programming.  Don't limit yourself to poor practices to solve something that can easily be handled in a loop.  Write code you can maintain, scale, and read.  If loops are the best option, use them.  You're essentially saying "How can I use this screw if I don't want to use a screwdriver?"  Sure, you can hammer it in.  But, stop being silly.  Use the tool meant for the job.

0 Kudos
Message 2 of 10
(12,619 Views)

ya i realy accepct your answer.but whenever i use loop my program getting slove that y i am avoiding loop.

0 Kudos
Message 3 of 10
(12,613 Views)

@GokulGKM wrote:

whenever i use loop my program getting slove that y i am avoiding loop.


That is likely due to an inefficient loop usage.  Mind sharing your code for that?

 

I don't think the In Place Element Structure was available in 8.2.  But here is some code that should do the reversing with as little memory allocations as possible.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 10
(12,591 Views)

Hi crossrulz,

 

why not this way:

check.png

Do you think this will need more memory allocations?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 5 of 10
(12,569 Views)

@GerdW wrote:

Hi crossrulz,

 

why not this way:

check.png

Do you think this will need more memory allocations?


I was worried about the memory allocations.  And being in an older version of LabVIEW (8.2), not as many compiler optimizations exist.  So to the brute force in place method I went.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 10
(12,552 Views)

GerdW wrote:

Do you think this will need more memory allocations?


I would not be surprised if both create very similar compiled code. Both version allow parallelization of the FOR loop, whcih could be important for very large arrays. Despite the shift register, the compiler can reduce Tim's code to a recognized transform that can be parallelized. This is an important clue.

 

Long ago, I posted examples for all possible 2D array transformations and back then I was very careful to limit the number of buffer allcations for the 2D array. I have just posted a second version that uses simpler code at the cost of a few buffer allocations. The original code was very efficient, always swapping two colums per iteration and not even touching the middle column for arrays with an odd number of columns. Unfortunately, this does not allow for loop parallelization and careful benchmarks would need to be made.

 

0 Kudos
Message 7 of 10
(12,544 Views)

Maybe not the most efficient way for very large arrays (didn't benchmarked it) but surely one of the simplest. Using the OpenG toolkit Reverse 2d Array_ogtk.vi.

 

Haven't looked if available for LabVIEW 8.2

 

reverse 2d array OpenG.png

 

Ben64

Message 8 of 10
(12,503 Views)

@crossrulz wrote:

@GerdW wrote:

Hi crossrulz,

 

why not this way:

check.png

Do you think this will need more memory allocations?


I was worried about the memory allocations.  And being in an older version of LabVIEW (8.2), not as many compiler optimizations exist.  So to the brute force in place method I went.


I would be pretty surprised if this fares really worse in even very old LabVIEW versions than the pull out, reverse and replace operation. LabVIEW optimization has been improved over the years but loop autoindexing was certainly one of the earliest optimization techniques, together with shift register optimization. After that much of the improvement were in different corner cases with the occasional hiccup of breaking other corner cases. They could sometimes mean dramatic improvements for very specific operations, but overall didn't make as much of an impact than the initial optimizations with shift register and autoindexing.

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 10
(12,440 Views)

I was going to suggest the OpenG method.  Yes it was available in 8.2, OpenG has changed little since the 7.x era.  That being said OpenG is rarely the most optimized way of doing this.  It usually goes on the side of compatibility and ease of use, but hasn't been optimized for modern improvements in a while.  Attached is the snippet of the reverse 2D array.

 

Reverse 2D Array (DBL)__ogtk_BD.png

0 Kudos
Message 10 of 10
(12,385 Views)