03-14-2015 04:04 PM - edited 03-14-2015 04:06 PM
Hi,
I have a following script
mu_B=zeros(1,21);
for i=1:21
count=i;
for j=1:125
mu_B(i)=mu_B(i)+a(j)*R(count);
count=count+21;
end
end
created in LabVIEW like this,
Not Getting required results and still I am not being able to figure out whether it's an exact equivalent of the script mentioned afore. Need Help.
Regards.
Solved! Go to Solution.
03-14-2015 05:31 PM
Please give us some values for a and R and the expected result for those values. Placing the values in the controls and making the values default before saving is the most convenient way to do this.
Lynn
03-14-2015 05:41 PM - edited 03-14-2015 05:43 PM
how big are the arrays [a] and [R]?
Your code makes very little sense. For example the central "index array" will always returns zero, so what's the point of having it there??
03-14-2015 05:50 PM
I wonder if your script is correct. The loop on j replaces mu_B[i] 125 times but only the last value is passed to the output.
Equivalent? mu_B[i] = mu_B[i] + a[124] * R[i + 125 * 20]. The index values are adjusted for the zero-based indexing used by LabVIEW. If this is equivalent to the script, then the inner loop is not needed.
Comment on your LV code: You could use autoindexing on the intialized array (mu_B?) rather than Array Index in the inner loop. Of couse since the value is always zero, neither the Index Array nor the Add is necessary.
Does the system in which your script runs modify the input array mu_B? LV does not work that way. You might need a shift register and Replace Array Subset.
Please clarify exactly what the script is expected to do.
Lynn
03-15-2015 01:20 PM
Your script describes a matrix multiplication, mu = R*a, where mu is a (column) vector of 21 elements, R is a matrix of 21 rows and 125 columns, and a is (column) vector of 125 rows. In your case, R is a 1D array, so we need to turn it into a 2D array with 21 rows and 125 columns. Once we do this, the problem is very simple to solve.
It will be a good exercise for you to do this for yourself (and we'll help, if you get stuck). There's a function on the Array palette called Reshape Array -- wire R in on the left, and your two indices, 21, then (pull down) 125), and you'll get the 2D form of R on the output. Bring R into a For loop -- do you notice the "indexing tunnel" (it looks like a square inside a square)? If you look at the wire inside the For loop, it is not a double-line, meaning a 2D array, but a single line, meaning its a row of the 2D array. You don't need to wire anything into the N terminal of the For loop -- it will automatically step through each of the 21 rows of R. Now bring a into the loop -- here you do not want an indexing tunnel (because you want the entire array, all 125 elements, inside the loop), so right-click the "a" tunnel and choose "Disable indexing". Now you have two 125-element 1D arrays you need to multiply, element by element -- this is exactly what the LabVIEW Multiply function does. Wire the output from this multiply through the For loop (notice another Indexing tunnel, this time turning each of the 21 iterations of the For loop into a 21-element array, "mu". And you are done.
So what do you need to solve this? One Reshape Array, one For loop, and one Multiply function. Try it out.
Bob Schor
03-15-2015 01:34 PM
Did a little work-out as was able to figure out the floorplan
03-15-2015 03:27 PM
Your version has one Initialize Array, two For (both with values wired to N), an Index Array, two Multiplies, and an Add. Plus it would take an experienced programmer some puzzling out to decide what you are trying to do (and to see, just from looking at the code, that you are doing it correctly). My way, with two functions and one For loop, is simpler and, more important, easier to understand. I urge you to try to code it, using the pretty explicit step-by-step instructions I provided. Once you've done that, try your method and my method on the same data, see if you get the same result. If not, which version would you like to debug?
Bob Schor
03-16-2015 02:18 PM
Did it and both giving same results. I guess lesser the number of loops, the faster the design is. I'll go with your architecture.
Thanks for help and another insight towards solving this problem. God bless you
Regards.
03-17-2015 08:12 AM
The key to developing better code is to step back and "see" more clearly the nature of the task you are trying to solve. I recognized from the script you showed that you were dealing with matrix/vector multiplication, so that's how I crafted my solution. I presume that you discovered that I left out a function -- it needs an "Add Array Elements" in addition to the Reshape, For, and Multiply.
Here's a slightly more general form that assumes the input arrays "a" and "r" are of "compatible" dimensions (the length of r needs to be a multiple of the length of a) and adds three more functions to determine the array dimensions of "a", "r", and, ultimately, "mu".
Bob Schor
03-17-2015 11:31 AM - edited 03-17-2015 11:31 AM
@Bob_Schor wrote:
Here's a slightly more general form that assumes the input arrays "a" and "r" are of "compatible" dimensions (the length of r needs to be a multiple of the length of a) and adds three more functions to determine the array dimensions of "a", "r", and, ultimately, "mu".
Or you could just dive into the linear algebra palette. Same difference. 😄