07-27-2022 11:50 AM
Hello! I am trying to compute a dot product between 2 arrays that each have an XYZ. I am wondering if there is an easy way to code this (similar to Matlabs ability to add dimensions to their dot product if you're familiar). I would rather not hard code it, so I figured I would ask before. I attached my code below. The end goal is to find the angle for the elbow joint, or the elbow between 3 3D points. Thank you!
07-27-2022 12:16 PM
In the Functions palette: Mathematics->Linear Algebra->Dot product.
07-27-2022 01:28 PM
Unfortunately that function does not work for my data set since it is not a vector, it is a matrix. I was hoping there was a quick fix so I wouldn't have to break down multiple arrays into X Y and Z.
07-27-2022 02:15 PM
You are confusing "mathematics" with "software implementation". A Dot Product is computed for two equal-length (n-dimensional) Vectors. In LabVIEW, such vectors are usually expressed as Arrays (which your code uses quite liberally). When expressed as a pair of Arrays, a Dot Product is easy to compute -- bring both Array "wires" into a For Loop (which, inside, turns them into sequential components), multiply the components, and bring the product "wire" to the right-hand edge of the For Loop structure, generating an "Indexing" tunnel (just like the ones on the Input side) and turning the Product in an Array of Products. Pass this Array of Products through a "Sum" operator for Arrays and you have the dot product -- much easier than Matlab, fits the definition of the Dot Product, and (I'm certain) runs significantly faster than Matlab. But if you really want to use Matlab, go right ahead.
You also mentioned how simple "Norm" was in Matlab. LabVIEW doesn't try to provide all of the Math functions (it is an "Engineering Workbench", after all, not a "Mathematics Workbench"), but see what you'd think this Snippet should be named --
If I needed to do a lot of Vector Norms in my LabVIEW code, I'd create the above-VI, name it something sensible (like "Vector Norm"), make it an Inline routine so it takes almost no memory space and runs at maximum speed, and be done with it.
Bob Schor
07-27-2022 02:22 PM
Your code is messy; and some of it makes no sense (indexing the same element multiple times).
Attached is what I think you want.
As for the 3D dot product: Just make a SubVI.
07-27-2022 03:46 PM - edited 07-27-2022 03:47 PM
I agree with @paul_cardinale and suggested exactly how to write such a sub-VI. Again, if you create it to run as an "inline sub-VI" (so it is compiled as machine code right into the VI that calls it), it will take a VI-icon's worth of Block Diagram space and will run as fast as LabVIEW's optimizing C++ compiler can get the code to run, which is probably noticably faster (and just as "compact") as Matlab. But, again, it works on vectors, which are usually implemented as an "array of vector components", very rarely as a Matrix (and, if you did, it would be a 1 x n or an n x 1 Matrix).
Bob Schor
07-27-2022 03:50 PM
Perhaps it doesn't make sense because I didn't attach the data. It is motion capture data that I have to delete things out of before analyzing. I can attach it below if it would help clarify for you. I'll just make a sub VI, just wanted to check if there was a simpler solution prior.
07-27-2022 03:54 PM
Yes, I know how a dot product works and I was simply checking to make sure I wasn't missing a built in function, such as the ones MATLAB provides. I am doing this project in both LabVIEW and MATLAB as a study on the benefits of both tools. I will make a sub VI that does that, but it is unfortunate LabView does not provide a way to do this without creating a sub VI. Thanks
07-27-2022 03:54 PM
@Bob_Schor wrote:
You are confusing "mathematics" with "software implementation". A Dot Product is computed for two equal-length (n-dimensional) Vectors. In LabVIEW, such vectors are usually expressed as Arrays (which your code uses quite liberally). When expressed as a pair of Arrays, a Dot Product is easy to compute -- bring both Array "wires" into a For Loop (which, inside, turns them into sequential components), multiply the components, and bring the product "wire" to the right-hand edge of the For Loop structure, generating an "Indexing" tunnel (just like the ones on the Input side) and turning the Product in an Array of Products. Pass this Array of Products through a "Sum" operator for Arrays and you have the dot product -- much easier than Matlab, fits the definition of the Dot Product, and (I'm certain) runs significantly faster than Matlab. But if you really want to use Matlab, go right ahead.
You also mentioned how simple "Norm" was in Matlab. LabVIEW doesn't try to provide all of the Math functions (it is an "Engineering Workbench", after all, not a "Mathematics Workbench"), but see what you'd think this Snippet should be named --
If I needed to do a lot of Vector Norms in my LabVIEW code, I'd create the above-VI, name it something sensible (like "Vector Norm"), make it an Inline routine so it takes almost no memory space and runs at maximum speed, and be done with it.
Bob Schor
Don't need the loop there. The "Square" function (like most math fns) will work with arrays.
07-27-2022 04:45 PM
@Bob_Schor wrote:
You are confusing "mathematics" with "software implementation". A Dot Product is computed for two equal-length (n-dimensional) Vectors. In LabVIEW, such vectors are usually expressed as Arrays (which your code uses quite liberally). When expressed as a pair of Arrays, a Dot Product is easy to compute -- bring both Array "wires" into a For Loop (which, inside, turns them into sequential components), multiply the components, and bring the product "wire" to the right-hand edge of the For Loop structure, generating an "Indexing" tunnel (just like the ones on the Input side) and turning the Product in an Array of Products. Pass this Array of Products through a "Sum" operator for Arrays and you have the dot product -- much easier than Matlab, fits the definition of the Dot Product, and (I'm certain) runs significantly faster than Matlab. But if you really want to use Matlab, go right ahead.
You also mentioned how simple "Norm" was in Matlab. LabVIEW doesn't try to provide all of the Math functions (it is an "Engineering Workbench", after all, not a "Mathematics Workbench"), but see what you'd think this Snippet should be named --
If I needed to do a lot of Vector Norms in my LabVIEW code, I'd create the above-VI, name it something sensible (like "Vector Norm"), make it an Inline routine so it takes almost no memory space and runs at maximum speed, and be done with it.
Bob Schor
A VECTOR is one dimension of a MATRIX. just use the MATRIX dot product function! Your math is good!