LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

When add multiple new rows to an array - is there a "best practice" to use here?

Solved!
Go to solution

I need a changing array.  It isn't going to change much, but it has to be done.

 

In MatLab I would write:

Capture.PNG

Now I don't expect this to be pretty.  I know I could wrap it in MathScript if I wanted a 1000x hit in performance (link), so I'm trying to avoid that.

 

If I were to do the following in LabVIEW, would it bomb in some way that my MatLab intuition does not inform?  Is there a more preferred way to do that?

 

Capture.PNG

Note: the inputs are the same between the VI and the MatLab script.

0 Kudos
Message 1 of 10
(5,444 Views)

There are numerous questions.  I assume you know that DataRow and A must have the same number of columns.  Let's consider three cases:

 

RowIdx is an index of an existing row in A (LabVIEW uses 0-offset indexing, so if A has 5 rows, RowIdx is 0 .. 4 in this case).  Use the LabVIEW function Replace Array Subset, wiring DataRow and RowIdx in the appropriate spots.

 

RowIdx is the "next" row of A, i.e. we are appending to the end of A.  In the above example, RowIdx is 5.  Use Build Array, wire A to the first input and DataRow to the second.

 

RowIdx is even larger (I'm ignoring RowIdx is negative ...).  Do you want to append blank rows and then DataRow?  You can certainly do that, using the same Build Array, and using Initialize Array to create the blank rows.

 

You can wrap these "cases" up in a Case Statement where you compare RowIdx with the first element of the Array Size output (to get the # rows).

 

Bob Schor

0 Kudos
Message 2 of 10
(5,419 Views)

Although my row-numbers are in the range of 1-500k, so not too big, I don't know that "build" works as a solution.  There is no way to know ahead of time if I need to wire in one row, or 1000 rows of zeros in between.  I could build it as a for-loop if you recommend that.  Do you?

 

It would also be helpful in this case if "sparse" was on the menu in LabVIEW.  

 

A negative row index is like a negative probability for me: non-physical.

0 Kudos
Message 3 of 10
(5,409 Views)
Solution
Accepted by topic author EngrStudent

I would just subtract the number of rows from the desired index and Initialize Array with that difference number of rows.  Use the number of columns for the second dimension.  Then Build Array and Replace Array Subset.


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
Message 4 of 10
(5,395 Views)

Instead of the "initialize array..built array" combo, you can also use "reshape array". Fewer primitives! 😄

Message 5 of 10
(5,380 Views)

Very elegant, Crossrulz.  Your algorithm is basically the same as mine, but you do it without Case Statements, letting LabVIEW detect when to append an Empty Row (my Case 1) before doing the Replace, and doing an Empty Row Insert-and-replace in my Case 2.  [Had to work through the three cases to convince myself we had the same idea ...].

 

Bob Schor

0 Kudos
Message 6 of 10
(5,375 Views)

@altenbach wrote:

Instead of the "initialize array..built array" combo, you can also use "reshape array". Fewer primitives! 😄


That would require a comparison of some sort.  Though, a Max & Min could do it...  Time to start playing again...


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 7 of 10
(5,370 Views)

@crossrulz wrote:

@altenbach wrote:

Instead of the "initialize array..built array" combo, you can also use "reshape array". Fewer primitives! 😄


That would require a comparison of some sort.  Though, a Max & Min could do it...  Time to start playing again...


You would still do something like your blue calculation to determine the final size. (Of course nothing would work if we need to pad by columns).

I think the community has example code that does all this (replace row/column & pad if necessary), but I cannot find it at the moment.

0 Kudos
Message 8 of 10
(5,360 Views)

Yep, one less primitive going with the Reshape Array instead of the Initialize Array and Build Array.


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
Message 9 of 10
(5,357 Views)

exactly what I had in mind. 😄

Message 10 of 10
(5,338 Views)