LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

1d array subtraction from each column in a 2d array

Hi,
I am trying to subtact a 1d array from a each column in a 2d array and i'm not quite sure the best way to do it.  Once the subtraction occurs I would then like to sum each column  of the results and put it into a 1d array.

for example

1d array  =
2
2
2

2d array =
3    4    5
3    4    5
3    4    5

1 d array Results =

3
6
9

Thank you

0 Kudos
Message 1 of 8
(6,582 Views)

The two simplest methods are probably like this. They have subtle differences which will only be relevant in the case of very large arrays (one of them being that the second method was wrong, so I removed it Smiley Happy but you can just use the first one).

To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. Here, here, here, here and here are a few you can start with and here are some tutorial videos. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide and the LabVIEW user manual (Help>>Search the LabVIEW Bookshelf).

Message Edited by tst on 02-04-2007 01:24 AM


___________________
Try to take over the world!
Message 2 of 8
(6,571 Views)

(Tst, I agree with that solution. The one you just deleted was wrong. :D)

However, I despise input terminals inside loops, because data could change at an arbitrary iteration while the loop executes, giving unpredictable results. 😉

If you place the 1D array terminal before the loop as shown in this image, there will also be a performance advantage, important for large datasets (not important here with a 3x3 array). (1) The terminal only gets read exactly once instead of at each loop iteration (an expensive operation involving the UI!) and (2) the compiler can fold the 1D array into a constant during the loop execution (see the grey outline showing folding in LabVIEW 8.20?).

 

Message Edited by altenbach on 02-03-2007 03:53 PM

Message 3 of 8
(6,561 Views)
If you don't want to transpose first, here are two alternative possibilities. Notice that we now need to auto index on both arrays.
 

If you're dealing with large arrays, it is always worth to do a few variations, then race them against each other (And ensure they all give the same result ;)).
 
Maybe omitting the transposition is beneficial. My guess it is, because it needs to touch all elements in the 2D array. The proof would be a benchmark test, which I haven't done.

Message Edited by altenbach on 02-03-2007 05:11 PM

Message 4 of 8
(6,540 Views)
Look Ma, no transposing and no autoindexing!
 
 
And this time I did actually check to see that it works...

@altenbach wrote:

(Tst, I agree with that solution. The one you just deleted was wrong. :D)


What are you talking about? I don't see any other solution... Smiley Wink


However, I despise input terminals inside loops, because data could change at an arbitrary iteration while the loop executes, giving unpredictable results. ;)

That was just for clarity (remember the Display Terminals as Icons thing?), because I found some people have trouble seeing auto-indexing tunnels. I definitely agree with you on this point. Also, the transposition (If I remember correctly) is also an expensive operation because it has to rearrange the data in memory (I think, needs to be tested).

Anyway, I doubt the OP will need to deal with very large arrays, but we've already been through this recently.

___________________
Try to take over the world!
Message 5 of 8
(6,531 Views)

I consider these LabVIEW minaitures what some might call "Fingerübungen" for the LabVIEW programmer. It's all fun. 🙂

I simply hope that it will peak the interest of some aspiring programmers that happen to pass by. Practical examples are  the only way to learn well about loops, tunnels (autoindexing and otherwise), and the fundamental truth that there is always more than one way to do things.

I've never tested the speed diffference between slicing out a row vs. a column, but my gut feeling tells me that a row (= autoindexing) is probably slightly better because elements in a row are immediately adjacent in memory, while elements in a column are spaced apart.

The OP gave us an exceedingly simplistic case with a square 2D matrix, but all our solutions also work for the general case where the 2D array can be of arbitrary size. The 1D input array must match the column size and the result will match the row size.

0 Kudos
Message 6 of 8
(6,523 Views)

If only the final result is important to you, consider the following solution too!

 

 

A new Item for the wish list: I wish there was a property for auto-indexing node, asking the user on which dimention it must act. therefore we didn't need to transpose the input array and make a new copy of it, spending a lot of memory space and time. Although there may be some way to create the input 2D array transposed Smiley Happy in the earlier stage, where it is built.

Message Edited by Soroush on 02-04-2007 03:47 AM

Message 7 of 8
(6,521 Views)
Very nice! 🙂
0 Kudos
Message 8 of 8
(6,513 Views)