LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Insert a 2D array into another 2D array at specific locations

Hi,

 

My problem:

I have a 2D array of dimension 3x3. I want to insert this array into a bigger array of dimension 5x5 (having all elements zero, say) at alternate positions.

More clearly,

 

My 3x3 array is suppose: [1 2 3

                                          4 5 6

                                          7 8 9]

 

My final array (5x5 array) should look: [1 0 2 0 3

                                                               0 0 0 0 0

                                                              4 0 5 0 6

                                                              0 0 0 0 0

                                                              7 0 8 0 9]

I will be very grateful, if somebody helps me in this regard.

Thanking You!

 

0 Kudos
Message 1 of 6
(3,614 Views)

Maybe this help you

array.png

0 Kudos
Message 2 of 6
(3,602 Views)

Think like a mathematician and solve an easier problem.

 

Consider a 1D array, [1 2 3] that you want to transform into another 1D array, [1 0 2 0 3].  Here is an easier problem -- transform the [1 2 3] array into [1 0 2 0 3 0] (a simple FOR loop where you replace a single element with a 2-element 1D array, [x 0], and use a concatenating output tunnel does the trick).  Now just delete the last element!

 

Now that we have solved the 1D problem, extend it to a 2D problem.  The inner loop creates a single row (of either 5 or 6 elements), and the outer loop assembles the rows into a 2D array by appending a row of zeros to each single row and outputting through a concatenating tunnel.  You then trim the final (zero) row and possibly the final column if you didn't do in "inside the loop".

 

The other suggestion here will probably work (I didn't test it, but assume it wouldn't have been posted if it didn't work).  However, just looking at it, I couldn't easily see what it was doing, so have to "take it on faith" that it works.  My algorithm, particularly with a comment or two, should be "obviously correct" to someone who knows a little LabVIEW.

 

Bob "Do the Math" Schor

 

Bob Schor

0 Kudos
Message 3 of 6
(3,576 Views)

What about stupid replace at index x 2?

No big array resizing multiple times.

expand array.png

Message 4 of 6
(3,567 Views)

A slight improvement would be to scale both array dimensions by X2-1 independently, by doing this the algorith would work even if the initial array number of rows and columns are different.

 

Ben64

0 Kudos
Message 5 of 6
(3,550 Views)

Yep, the Alexander/Ben "direct" method is probably optimal ...

 

Bob Schor

0 Kudos
Message 6 of 6
(3,525 Views)