LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to insert an element into a specific cell on a 2D array? (See details and attached code)?

Solved!
Go to solution

My goal is to insert letter i into second row and fifth column. The insert into array is not really working for me. 

Below is the result I want:

GRCK5000_0-1718055035240.png

 

Below is how my code looks like:

GRCK5000_1-1718055239258.png

 

 

 

0 Kudos
Message 1 of 4
(579 Views)
Solution
Accepted by GRCK5000

You have a couple issues here. Consider how you might want this to work. You're inserting a 1D array (of 1 element) at the end of this array, so it puts it at the top. If you only have one element, how would LabVIEW know to put it at the bottom? It doesn't; it puts it at the top.

 

Also, LabVIEW arrays cannot have "null" elements. You start with a 2x4 array and add one element, so now it HAS to be a 2x5 array. What should go into that other spot?

 

If you don't specify an element (as you've done) it will automatically pad out the data with the default data type here, which is an empty string.

 

All you have to do is make the new array you insert into the array you actually want to insert, which includes an empty element before your "i":

 

Insert_Into_2DArray.png

 

Also, don't forget you're using Insert into Array, not Replace Array Element. "Insert" will make room for a new element by moving around other elements to make it fit. This means you MUST give it a 1D array, because how else would it know to make room?

 

Also, if you use Replace Array Element you will still need to pad your array, since Replace can't replace an element that isn't already there.

 

Hope that helps.

Message 2 of 4
(568 Views)
Solution
Accepted by GRCK5000

Obviously, all rows must have the same number of elements, so instead of constantly growing your array whenever you try add something outside the current bounds, start with a 2D array that is sufficiently sized for the duration of the run. This is much more efficient, so if you think you'll eventually end up with a 10x10 array, why not initialize it as such??

 

altenbach_0-1718069887051.png

 

Of course you can create your own "replace array element" that would automatically pad and resize as needed. You could even make it a vim. I am sure somebody already wrote that. I like performance and don't like to hammer the memory manager with menial tasks. Since arrays need to be contiguous in memory order, inserting a column requires a fresh allocation where all elements need to be moved to a new memory locations. Expensive!

 

See also our talk form many years ago (part II, slide 11) shown below:

 

altenbach_1-1718070393581.png

 

 

 

Message 3 of 4
(534 Views)

Thank you, BertMcMahan and Mr. Altenbach for sharing these great information and the codes! I'm also going to finish watching the talk form many years ago . I watched it a little bit and it's very informative. 

0 Kudos
Message 4 of 4
(490 Views)