LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculating a dot product with two 3D Arrays ( X Y Z components)

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!

0 Kudos
Message 1 of 16
(1,739 Views)

In the Functions palette: Mathematics->Linear Algebra->Dot product.

Marc Dubois
0 Kudos
Message 2 of 16
(1,731 Views)

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.

0 Kudos
Message 3 of 16
(1,709 Views)

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 --

Vector Norm.png

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 

0 Kudos
Message 4 of 16
(1,702 Views)

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.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 5 of 16
(1,700 Views)

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

0 Kudos
Message 6 of 16
(1,688 Views)

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.

0 Kudos
Message 7 of 16
(1,684 Views)

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

0 Kudos
Message 8 of 16
(1,680 Views)

@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 --

Vector Norm.png

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.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 9 of 16
(1,679 Views)

@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 --

Vector Norm.png

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!


"Should be" isn't "Is" -Jay
0 Kudos
Message 10 of 16
(1,658 Views)