[英]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). 设A是任意维数的数组(在我的例子中为2或3)。 How can I flatten (reshape to 1D) this array without knowing in advance the number of dimensions ?
如何在不事先知道尺寸数量的情况下展平(重塑为1D)此阵列?
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. 如果我知道尺寸的数量,我可以很容易地获得元素的总数(通过使用“数组大小”功能的组合),然后使用此数字重新形成A到1D。 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. 我已经通过创建一个函数(VI)“解决”了这个问题,该函数返回了3D数组的元素总数(我期望处理的最常见的数组类型),所以我可以将它作为参数给出重塑功能。 Problem: it won't accept a 2D array, even though the algorithm would work with such an array !
问题:它不会接受2D数组,即使算法适用于这样的数组! (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).
(似乎在Labview中,数组控件对它们接受的维数是严格的,例如在Matlab中不是这种情况)。
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. 一个很好的解决方案是将“数组大小”给出的数组的所有元素相乘以快速获得元素总数的简单方法,而不必将其包装在子VI中。 Overall, isn't there a simple and efficient way to solve this problem ?
总的来说,是不是有一种简单有效的方法来解决这个问题? It should be quite standard..
应该很标准..
I belive this is what you are looking for : 我相信这就是你要找的东西:
http://zone.ni.com/reference/en-XX/help/371361E-01/glang/reshape_array/ http://zone.ni.com/reference/en-XX/help/371361E-01/glang/reshape_array/
You would do : 你会这样做:
n-DimInputArray --> ArraySize --> 1D_SizesOUT n-DimInputArray - > ArraySize - > 1D_SizesOUT
This returns a 1D array containing the sizes of all of the array dimensions. 这将返回包含所有数组维度大小的1D数组。
You then go : 然后你去:
1D_SizeOUT --> MultiplyArrayElements --> NumberOfElementsIn_n-DimInputArray 1D_SizeOUT - > MultiplyArrayElements - > NumberOfElementsIn_n-DimInputArray
This value goes in as the dimension size for ReshapeArray - done. 此值作为ReshapeArray的尺寸大小 - 完成。
http://zone.ni.com/reference/en-XX/help/371361E-01/glang/array_size/ 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/ http://zone.ni.com/reference/en-XX/help/371361E-01/glang/multiply_array_elements/
Here's a snippet of the above code: 以下是上述代码的片段:
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.
在subvi中,“Variant to Data”包含您的维度数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.