LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Split an array in equal parts

Hi all,

 

I have a question about arrays. I do not have a code yet because I have no idea how and where to start. The question is how do I separate a 1D array in equal parts? I want the user to be able to set the amount of parts the array has to be split in. So lets say I have an array of 12 elements, I want to let the user decide if the array should be split in 2,3, or 4 equal parts. Also, I want to make it possible to let the user select a number that 12 can't be divided with, but it wil still display an amount of equal parts plus the remainder. Then I want to do calculations with each of the subarray: I need the maximum, minimum and average. (I already know how to do that, by using 'Max&Min' and ' add array elements' divided by 'array size' 🙂 )

I have found two functions that could possibly do what I want. One is using ' split 1D array' in a loop, but I think it can be done easier than this. And that would be by using 'Array subset'. This function seems to do exactly what I want, and when I look it up it seems that you can make this function longer by pulling it. But when I want to do that, the icon to make it longer doesn't show. I'm using labview 2011.

Sorry if the question is a bit stupid,and if the question is a bit long, still a newbie. But I'm very eager to learn!

Thanks in advance!

0 Kudos
Message 1 of 5
(5,493 Views)

Hi LvL,

 

This function seems to do exactly what I want, and when I look it up it seems that you can make this function longer by pulling it. But when I want to do that, the icon to make it longer doesn't show. I'm using labview 2011.

That's a mistake in the help. It only shows that ArraySubset will adapt to the dimension of the array you connect at the input…

 

Use either SplitArray of ArraySubset in a loop to get all the parts of your input array. This will make your VI scalable to array size and number of subset you want to calculate…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 5
(5,482 Views)

It sounds like you're on the right path. You might also consider using the reshape array function to transform your 12 element 1D array into a 2D array with dimensions of 2x6 or 3x4 (or 6x2 or 4x3). Then each row can be indexed in a for loop, and your min/max/avg functions calculated for each row (or sub array).

 

For dealing with the remainder, you could perform an initial split on the array to remove the remainder portion, then do the 2D transform as described above.

 

The array subset function can't be manually dragged down. Instead it expands in size to match the number of dimensions of the array you've wired up to it.




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
Message 3 of 5
(5,471 Views)

Thanks both! It might take me a bit before I figure it out but this can get me going 🙂

I think I'm going to try for the loop.

0 Kudos
Message 4 of 5
(5,433 Views)

You can use the Array Size vi to get the size of the array and then the Split 1D array function with the index as the output of array size divided by 2.

0 Kudos
Message 5 of 5
(5,427 Views)