Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten (reshape to 1D) an array of arbitrary dimension in Labview

Let A be an array of arbitrary dimension (2 or 3 in my case). How can I flatten (reshape to 1D) this array without knowing in advance the number of dimensions ?

If I knew the number of dimensions, I could easily obtain the total number of elements (by using a combination of "Array Size" function) and then reshape A to 1D using this number. Unfortunately, the "Array Size" function itself returns an array whose number of elements is equal to the number of dimensions, which brings us back to the initial problem.

I have "solved" the problem by creating a function (VI) that returns the total number of elements of a 3D array (the most common type of arrays that I expect to handle), so that I can give this as an argument to the Reshape function. Problem: it won't accept a 2D array, even though the algorithm would work with such an array ! (it seems that in Labview, array controls are strict about the number of dimensions they accept, which isn't the case in Matlab for instance).

A nice solution would be a simple way to multiply all the elements of the array given by "Array Size" to quickly get the total number of elements, without having to wrap this in a sub VI. Overall, isn't there a simple and efficient way to solve this problem ? It should be quite standard..

like image 804
calvin tiger Avatar asked Jan 13 '12 11:01

calvin tiger


2 Answers

I belive this is what you are looking for :

http://zone.ni.com/reference/en-XX/help/371361E-01/glang/reshape_array/

You would do :

n-DimInputArray --> ArraySize --> 1D_SizesOUT

This returns a 1D array containing the sizes of all of the array dimensions.

You then go :

1D_SizeOUT --> MultiplyArrayElements --> NumberOfElementsIn_n-DimInputArray

This value goes in as the dimension size for ReshapeArray - done.

http://zone.ni.com/reference/en-XX/help/371361E-01/glang/array_size/

http://zone.ni.com/reference/en-XX/help/371361E-01/glang/multiply_array_elements/

Here's a snippet of the above code:
enter image description here

like image 192
J... Avatar answered Jan 02 '23 19:01

J...


Just get the array size of the array size and you'll get the number of dimensions...

To put this into a subvi, transform your unknown array into a variant and give this variant and the number of dimensions into the subvi. Inside the subvi, "Variant to Data" with an array of your dimension.

like image 30
Birgit P. Avatar answered Jan 02 '23 19:01

Birgit P.