10-26-2009 09:30 AM
A function for the efficiency aficionados out there, "Matrix Size". Matrices in LV have not really grown on me yet, but there are a couple of features that I like. A matrix is essentially a 2D array, and most matrix function complement the array functions. There are some nice linear algebra VIs available, but they seem to deal with 2D arrays as well. A coercion occurs, but it doesn't seem to involve a buffer allocation. There is always the Array to Matrix VI (helps calls it a VI, looks like a function to me). One thing I do like is the simple look of the array control once I dump the scrollbars, dropping the increment/decrement buttons from an array control helps, but there is still some times when the 3D look is a bit much.
Now for the efficiency part. I use a lot of 2D arrays, and I am always getting the number of rows and columns. This normally requires the Array size Function followed by index array (which at least auto-indexes and expands now). With Matrix Size, you can get both values with one function and it happily takes your 2D array.
Aside: I removed the '2's from the indicator labels but it seems the snippet has put them back. I was used to this in my fragment modification since I use copy and paste, not sure why it happens here.
VIOTD groundrules here.
10-26-2009 10:32 AM
Matrix Size is quite handy.
10-26-2009 11:28 AM
Darin.K wrote:the Array to Matrix VI (helps calls it a VI, looks like a function to me).
Did you try double clicking it? The result might interest you, if I remember correctly.
Darin.K wrote:
With Matrix Size, you can get both values with one function and it happily takes your 2D array.
I wasn't aware of that. Certainly sounds useful, even if turns out to also just be a VI. I never used matrices, and to this day, I still don't understand how they differ from 2D array, although I can't say I ever tried to find out.
10-26-2009 11:38 AM
tst wrote:
Darin.K wrote:the Array to Matrix VI (helps calls it a VI, looks like a function to me).
Did you try double clicking it? The result might interest you, if I remember correctly.
Darin.K wrote:
With Matrix Size, you can get both values with one function and it happily takes your 2D array.I wasn't aware of that. Certainly sounds useful, even if turns out to also just be a VI. I never used matrices, and to this day, I still don't understand how they differ from 2D array, although I can't say I ever tried to find out.
Array to Matrix is in fact a VI disguised as a function. A lot of effort to hide a little coercion dot (at least for DBL to Real Matrix). The only difference between matrices and 2d arrays I can discern is that matrices supposedly return NaN (DBL) if you use an out of range index. Never tried it, and I don't think I will in the near future.
10-26-2009 12:17 PM
I believe that function only exists in LV 2009 (9.0). I know it's not in 8.2 otherwise I'd be using it instead of the alternate method as noted above.
Perhaps it would be a good idea to have an "Applies to ..." part in your VI of the day postings.
10-26-2009 12:26 PM
Perhaps that is a reason to upgrade to LV2009.
Just doing my sales rep impression there, although I must say this is a better reason than others I have seen...
I try to point out LV9-only functions/features (and mostly try not to have too many in a row), I just assumed this one had flown under my radar and was not available sooner. At least when I ask myself, why I wasn't using this sooner, I have an answer.
10-26-2009 12:30 PM
Darin,
Math operators (specifically division and multiplication operators) behave differently with Matricies and 2d Arrays.
Some are the same some are different. I woud have to go find my math book on matricies to tell you why, lol (I think I had a Linear Algebra class that went over this kind of stuff back awhile ago).
10-26-2009 12:37 PM - edited 10-26-2009 12:38 PM
smercurio_fc wrote:I believe that function only exists in LV 2009 (9.0). I know it's not in 8.2 otherwise I'd be using it instead of the alternate method as noted above.
Perhaps it would be a good idea to have an "Applies to ..." part in your VI of the day postings.
Aw, man. I was all looking forward to using this VI now. Just another reason to upgrade I guess.
Two messages were posted before I finished typing this one.
10-26-2009 12:40 PM - edited 10-26-2009 12:41 PM
Nickerbocker wrote:Darin,
Math operators (specifically division and multiplication operators) behave differently with Matricies and 2d Arrays.
Some are the same some are different. I woud have to go find my math book on matricies to tell you why, lol (I think I had a Linear Algebra class that went over this kind of stuff back awhile ago).
You are correct sir. Matrix multiplication is different than element-by-element multiplication, non-commutitive and all that good stuff. It appears that for arrays, LV removes the multiplication function, replaces it with the AxB.vi for matrices and leaves the icon intact. Good to know, the few times I have used matrix multiplication I went straight for the vi, I like the look of the multiplication function better.
10-26-2009 12:57 PM - edited 10-26-2009 01:04 PM
Very cool tip...I was not aware of the Matrix Size function. Perhaps I will steal borrow this tip for a future Darren's Weekly Nugget. 😉
I just benchmarked the two approaches, and it looks like Matrix Size is slightly slower than the Array Size -> Index Array approach, but unless you're calculating 2D array sizes in a very tight loop, it shouldn't make enough of a difference to account for the usability and real estate advantages of Matrix Size.
-D
Edit: Whoa! I just discovered that it works for *any* 2D array...it doesn't even have to be Numeric! This may just be cool enough to be today's nugget. 🙂