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

calvin tiger picture calvin tiger · Jan 13, 2012 · Viewed 10k times · Source

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

Answer

J... picture J... · Jan 13, 2012

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