04-04-2016 11:19 AM - edited 04-04-2016 11:47 AM
I need a changing array. It isn't going to change much, but it has to be done.
In MatLab I would write:
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?
Note: the inputs are the same between the VI and the MatLab script.
Solved! Go to Solution.
04-04-2016 12:11 PM
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
04-04-2016 12:17 PM - edited 04-04-2016 12:18 PM
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.
04-04-2016 12:35 PM - edited 04-04-2016 12:36 PM
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.
04-04-2016 12:45 PM
Instead of the "initialize array..built array" combo, you can also use "reshape array". Fewer primitives! 😄
04-04-2016 12:49 PM
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
04-04-2016 12:50 PM
@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...
04-04-2016 12:55 PM
@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.
04-04-2016 12:56 PM - edited 04-04-2016 12:57 PM
Yep, one less primitive going with the Reshape Array instead of the Initialize Array and Build Array.
04-04-2016 01:04 PM
exactly what I had in mind. 😄