From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Split array to different sizes

Solved!
Go to solution

Hello.

 

I have been stuck with this for a couple of days now. 

 

I have an array that I want to split into different sizes and store them.

 

The array I want to split Y = [1 2 3 4 5]

Then I have another boolean array (B) the same size as Y. This boolean array shall decide how to split Y.

 B = [F T T F T]

 

My output that I am seeking would then be 2 arrays [2 3]  and [5]

 

Since they are different sizes I think the best way would be to input the resulting arrays to a cluster containing an array?

But I have no more ideas on how to this. I have tried to used for loops with the splitting but end up with problems with index 0 and the storing of the results.

 

Is there anyone that have faced the same problem and could point me in the right direction?

 

Best regards

Peter

 

0 Kudos
Message 1 of 7
(2,827 Views)

Hi CLD,

 


@CLDmannen wrote:

Since they are different sizes I think the best way would be to input the resulting arrays to a cluster containing an array?


Yes.

In a 2D array all rows/columns have the same size, but in a 1D array of cluster of 1D array the "inner" arrays can have different length…

 


@CLDmannen wrote:

The array I want to split Y = [1 2 3 4 5]

Then I have another boolean array (B) the same size as Y. This boolean array shall decide how to split Y.

 B = [F T T F T]

My output that I am seeking would then be 2 arrays [2 3]  and [5]


Algorithm:

  1. Use a while loop
  2. search for "T" in your boolean array: its index will be "start"
  3. search for "F" in your boolean array: its index will be "end+1"
  4. use ArraySubset(Y, start, end+1-start) to get a subarray
  5. repeat the loop to continue the search…

Add some error handling on your own…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 7
(2,817 Views)

Thank you for your reply!

 

But I think you underestimated the problem alittle. (As did I)

 

CLDmannen_0-1590399273179.png

This solution is semi-according what you wrote. As you can see I get issues with the 0 index and for the next ittereation I would need to remove all "F" in the array or the search function will find the END element before the start element. This gets really fast a messy solution. 

I will try to make it work and post when successful

 

Best regards

Peter

0 Kudos
Message 3 of 7
(2,735 Views)
Solution
Accepted by topic author CLDmannen

Hi Peter,

 


@CLDmannen wrote:

But I think you underestimated the problem alittle. (As did I)

 

This solution is semi-according what you wrote. As you can see I get issues with the 0 index and for the next ittereation I would need to remove all "F" in the array or the search function will find the END element before the start element. This gets really fast a messy solution. 


I don't think so…

At Search1DArray there is one more input: the index where to start with the search. Add one more shift register to your loop and the search and split will become very easy…

 

Why does your WHILE loop stop immediatly after the first iteration? I thought you want to find more than one block of TRUE bits!?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 7
(2,732 Views)

Yes, that is true I want to find all I did not have the time to finsih that part.

Good point with the start index, I had missed that. Will try.

 

/Peter

0 Kudos
Message 5 of 7
(2,725 Views)

Thank you!

 

This is the final solution

CLDmannen_0-1590402975209.png

 

0 Kudos
Message 6 of 7
(2,714 Views)

Hi Peter,

 


@CLDmannen wrote:

This is the final solution


You can simplify your VI:

  • no need to wire a boolean constant to the loop stop terminal: just use the output of the comparison function directly…
  • use a conditional output tunnel of the loop to build your array: this is available since several years/LabVIEW versions (no need for the array constant, shift register and BuildArray)…
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 7
(2,694 Views)