LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to move rows in an array without allocating new memory.

Hello,

I would just like to move lets say row '2' an '3' behind the '4'

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

Is it possible to do it without deleting the row and inserting it to new position since that causes memory deallocation/allocation? I was thinking if 'In place structure' has such an option but it doesn't. Attached solution is the common way to do that. Is there better way regarding the prformance?
LV 2011, Win7
0 Kudos
Message 1 of 17
(5,149 Views)

HI ceties,

 

as usual there is a trade-off between speed and memory needs Smiley Wink

 

To minimize buffer allocations you can use the traditional swap operation, here shown in text-based syntax:

swap(array;row1;row2)

{

 help[]:= array[row1]

 array[row1]:=array[row2]

 array[row2]:=help[]

}

 

Your vi will need two buffers of size columns(array) and you could use "simple" IndexArray and ReplaceArraySubset functions. For your question you would call swap two times: swap(array;3;4) and swap(array;2;3) in exactly this order...

And yes, right now the InPlaceStructure doesn't support to work on columns/rows in place - you have to index single array elements. But there is an idea in the exchange to vote for...

Message Edited by GerdW on 12-21-2009 10:28 AM
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 17
(5,138 Views)

Take a look at the application->memory control palette there are some very under-utilized structures for moving memory locations in place.

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 3 of 17
(5,106 Views)

Hi Gerd andthanks. You are right with the trade off between speed and memory allocation -the swap operation is thus not very useful – just imagine I was moving  the line 2,3 not to instead of 4 but lets seto 99 – the amount of swap operations would be enormous.

If somebodyis interested why I needed that here is why

2 falkpl:There is nothing that would help on that palette. Hopefully in the futurereleases.

Message Edited by ceties on 01-08-2010 05:07 AM
LV 2011, Win7
0 Kudos
Message 4 of 17
(4,978 Views)

ceties wrote:
Is it possible to do it without deleting the row and inserting it to new position since that causes memory deallocation/allocation?

Yes, since the array does not change size, operations can be done in place, and all you need is a buffer for a single row, a loop, and the 2D array in a shift register. You only need to touch rows 2-4.

 

Another option would be to keep the 2D array untouched and just manipulate a lookup table of row indices. It really depends on how it's used and what you need it for.

 

In any  case, these methods will be orders of magnitude more efficient than delete from array + insert into array.

 

How big are the arrays and how often do you need to do all this?

Message 5 of 17
(4,945 Views)

Lookup table – I considered that but I would have to significantly alter the code of the application and one slight mistake would lead to a disaster (user would see different data than chosen in Listbox)

 

Swap the rows - what if I wanted to move the line 2,3 not instead of line 4 but lets say instead of line 99? – do you really think it would be faster than deleting and inserting? Swapping approach would lead to hundreds of line swapping operations if I count right. Or did I miss something?

 

The array is usually 20x3x3600 DBL. The operation is based on user interaction (Drag&Drop) and only once a while. Thanks for suggestions.

LV 2011, Win7
0 Kudos
Message 6 of 17
(4,887 Views)

I don't think you get the point of a swap - it's not that you swap one row for the next until you get to where you want. You simply take the row you want and replace the target row with it. This will create two additional buffers (one for each row), but the large array should remain the same.

 

Example_VI.png 

 

 

The in-place element structure will not be useful in this case, since it can't replace rows. I believe Altenbach has an idea for that in the exchange, so you can go vote it up there.


___________________
Try to take over the world!
0 Kudos
Message 7 of 17
(4,849 Views)

I definitely get the point of swapping but the problem is that pure swap doesn’t do the thing here. Imagine you have rows as follows:

 

0

1

2

3

4

5

6

7

8

9

 

I take rows1,2,6,7 and I want to move them behind the row 4 so I get the sequence

0

3

4

1

2

6

7

5

8

9

 

I am satisfied with my solution I was just wondering if there is some better since I was aware of slight memory unfriendliness of it.

Message Edited by ceties on 01-12-2010 07:04 AM
LV 2011, Win7
0 Kudos
Message 8 of 17
(4,825 Views)

I see. That's a different situation, because you have to move multiple elements.

 

Here are four options I can think of:

  1. Use your method. It's the simplest, and if your array isn't large or you're not doing this many many times, it's probably the best.
  2. Do multiple swaps, like you suggested. I would say this was only worth it if you were really concerned about memory consumption. You should consider it may take time to execute, depending on the number of swaps.
  3. Use the Array Subset primitive to get the entire section of the array you want to move and use the Replace Array Subset primitive to replace it in one call. The disadvantage is that you will probably get an allocation for that subset.
  4. The in-place structure seems to have a new option in 2009 - you can split an array and put it back together, although a quick test seems to indicate it expects the array sections to be the same length they were when you started (which would make sense).

___________________
Try to take over the world!
Message 9 of 17
(4,811 Views)
Thanks tst for the summary. I would also add the lookup table as Altenbach suggested. Plus no. 3 doesn't work for me since it works only for consecutive rows but my selection can have multiple elements that aren't consecutive. Thanks
LV 2011, Win7
0 Kudos
Message 10 of 17
(4,798 Views)