Discussion:
1d array subtraction from each column in a 2d array
(too old to reply)
tst
2007-02-03 23:40:06 UTC
Permalink
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 :smileyhappy: but you can just use the first one).
<img src="Loading Image..."><a href="http://forums.ni.com/attachments/ni/170/227805/2/Example_BD.png" target="_blank"></a>
To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. <a href="http://cnx.rice.edu/content/col10241/latest/" target="_blank">Here</a>, <a href="http://zone.ni.com/devzone/learningcenter.nsf/03f7c60f17aad210862567a90054a26c/55974411828f779086256ce9007504bd" target="_blank">here</a>, <a href="http://www.mech.uwa.edu.au/jpt/tutorial/index.html" target="_blank">here</a>, <a href="http://www.iit.edu/~labview/Dummies.html" target="_blank">here</a> and <a href="http://www.upscale.utoronto.ca/GeneralInterest/LabView.html" target="_blank">here</a> are a few you can start with and <a href="http://www.fafiles.com/" target="_blank">here</a> are some tutorial videos. You can also contact your local NI office and join one of their courses. In addition, I suggest you read <a href="http://zone.ni.com/devzone/conceptd.nsf/webmain/CB5E46406090C61C86256A7000559B66" target="_blank">the LabVIEW style guide</a> and the LabVIEW user manual (Help&gt;&gt;Search the LabVIEW Bookshelf). Message Edited by tst on 02-04-2007 01:24 AM


Example_BD.png:
http://forums.ni.com/attachments/ni/170/227805/2/Example_BD.png
altenbach
2007-02-04 00:10:07 UTC
Permalink
(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&nbsp;be a performance advantage, important for large datasets (not important here with a 3x3 array). (1) The terminal only gets read exactly&nbsp;once&nbsp;instead of&nbsp;at each loop&nbsp;iteration (an expensive operation involving the UI!) and (2)&nbsp;the compiler can fold the&nbsp;1D array into a constant during the loop execution (see the grey outline showing folding in LabVIEW 8.20?).

<img src="Loading Image...">
&nbsp;Message Edited by altenbach on 02-03-2007 03:53 PM


ArrayOperation.png:
http://forums.ni.com/attachments/ni/170/227807/1/ArrayOperation.png
altenbach
2007-02-04 01:10:08 UTC
Permalink
If you don't want to transpose first, here are two alternative&nbsp;possibilities. Notice that we now need to auto index on both arrays.
&nbsp;
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 ;)).
&nbsp;
Maybe omitting the transposition is beneficial. My guess it is, because it needs to touch all elements in the 2D array.


ArrayOps.png:
Loading Image...
tst
2007-02-04 08:10:07 UTC
Permalink
Look Ma, no transposing and no autoindexing!
&nbsp;
<img src="Loading Image...">
&nbsp;
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... :smileywink:



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.


Example_VI.png:
http://forums.ni.com/attachments/ni/170/227815/1/Example_VI.png
altenbach
2007-02-04 09:40:12 UTC
Permalink
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&nbsp; 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&nbsp;(= 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.
Soroush
2007-02-04 10:10:07 UTC
Permalink
If only the final result is important to you, consider the following&nbsp;solution too!
&nbsp;
<img src="Loading Image...">
&nbsp;
A new&nbsp;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&nbsp;make a new copy of it, spending a lot of memory space and time. Although there may be some way to create&nbsp;the input 2D array&nbsp;transposed&nbsp;:smileyhappy:&nbsp;in the earlier stage, where it is built.Message Edited by Soroush on 02-04-2007 03:47 AM


Solution.PNG:
http://forums.ni.com/attachments/ni/170/227819/1/Solution.PNG

Continue reading on narkive:
Loading...