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: 

Splitting array according to its index

Solved!
Go to solution

Hi all

 

I want to have a "demultiplexer" block that, having one (2Nx1) array as input, will split the data in two (Nx1) arrays as output wether the index of the first array is even or odd......

 

At first I tried to implement this with a simple case structure in a while loop:

 

ScreenShot003.jpg

 

 

But this doesn't work because I can ony run this .vi when the outputs of the case have option "Use default if unwired" set on. And this will produce not a pair of (Nx1) arrays, but a pair of (2Nx1) arrays. The first array will be [value, 0, value, 0, .....] and the second will be [0, value, 0, value....]

 

 

I want to get rid of those zeros....

 

Any hints?

 

Thanks in advance!

 

0 Kudos
Message 1 of 4
(3,841 Views)
Solution
Accepted by topic author captain_harlock

Try the decimate array function. That's probably all you need.

 

Here are a few general comments to your code in general:

  • Since the number of iterations is known before the loop starts, you need a FOR loop. No need to wire N or compare the iteration count. It will iterate for all elements in the autoindexing input array automatically, then exit the loop. Also, output indexing on a FOR loop is much more efficient, because the array sizes of the outputs can be determined and exactly allocated before the loop starts. With a while loop, labview cannot do that.
  • If you have LabVIEW 2012, you could use conditional indexing on the output tunnels.
  • You could wire the remainder directly to the case structure and make one case 1 and the other 0. Less code! (See my signature).
  • To check for odd vs. even, a logical AND with 1 is sufficient and more efficient.
  • The "-1" belongs before the loop because it needs to be done only once (well, the compiler recognized loop invariant code and will in fact create compiled code as if the "-1" were before the loop, but why not be explicit?)
  • When creating a code screenshot, try no to select anything.  These black dotted outlines are distracting.

 

0 Kudos
Message 2 of 4
(3,835 Views)

I feel like an idiot, I have been looking in the "array" functions three times...

Thanks, this is exactly what I was looking for.

 

 

 

0 Kudos
Message 3 of 4
(3,827 Views)

@altenbach wrote:

Try the decimate array function. That's probably all you need.

 

Here are a few general comments to your code in general:

  • Since the number of iterations is known before the loop starts, you need a FOR loop. No need to wire N or compare the iteration count. It will iterate for all elements in the autoindexing input array automatically, then exit the loop. Also, output indexing on a FOR loop is much more efficient, because the array sizes of the outputs can be determined and exactly allocated before the loop starts. With a while loop, labview cannot do that.
  • If you have LabVIEW 2012, you could use conditional indexing on the output tunnels.
  • You could wire the remainder directly to the case structure and make one case 1 and the other 0. Less code! (See my signature).
  • To check for odd vs. even, a logical AND with 1 is sufficient and more efficient.
  • The "-1" belongs before the loop because it needs to be done only once (well, the compiler recognized loop invariant code and will in fact create compiled code as if the "-1" were before the loop, but why not be explicit?)
  • When creating a code screenshot, try no to select anything.  These black dotted outlines are distracting.

 


Well actually what I used for screenshot was the code "epurated" from the off topic parts... in the While loop I do some other stuff so that's why it was there instead of a For.

About the -1 you're right, I just quickly added a part to give the While loop the exit condition because of the "other stuff" removed from it, so there was not that kind of optimization.

The use of (x AND 1) to decide if it is even or odd is a very nice idea...

0 Kudos
Message 4 of 4
(3,818 Views)