# Reduce 2D Array into 1D Sequence

## Recommended Posts

Hello,

I am working to build a data analysis system and having a little trouble with the arrays. I just want to reduce the data like below.

Example 2D array:

[ 1 3 5 7

2 4 6 8

1 3 5 7 ]

Into 1D Array:

[ 1 3 5 7 2 4 6 8 1 3 5 7 ]

I've been working on this for a couple of hours and it seems like a simple problem, but I'm not beating it yet.

Thanks,

##### Share on other sites

QUOTE (bradjb911 @ Apr 22 2009, 03:28 PM)

Hello,

I am working to build a data analysis system and having a little trouble with the arrays. I just want to reduce the data like below.

Example 2D array:

[ 1 3 5 7

2 4 6 8

1 3 5 7 ]

Into 1D Array:

[ 1 3 5 7 2 4 6 8 1 3 5 7 ]

I've been working on this for a couple of hours and it seems like a simple problem, but I'm not beating it yet.

Thanks,

Use re-shape array?

Multiple the dimensions of your 2d and use that value to spec the size of the 1-d array.

Ben

##### Share on other sites

QUOTE (crossrulz @ Apr 22 2009, 04:28 PM)

THe multiply array operator can replace the index and multiply code.

Ben

##### Share on other sites

Maybe I'm not thinking in G enough, but I would have just used an indexed For loop with a shift register and a Build Array. Your solutions are a little more concise, though.

##### Share on other sites

QUOTE (neBulus @ Apr 22 2009, 04:47 PM)

THe multiply array operator can replace the index and multiply code.

Ben

Dude, I have done this operation (index and multiply) so many times and not once did I even think of the Multiply Array. That will make it better.

##### Share on other sites

QUOTE (asbo @ Apr 22 2009, 05:34 PM)

...but I would have just used an indexed For loop with a shift register and a Build Array.

You mean "created an array with the appropriate number of elements outside a for loop, then replaced it's values", right?

##### Share on other sites

QUOTE (neBulus @ Apr 22 2009, 09:47 PM)

THe multiply array operator can replace the index and multiply code.

Ben

And means that you can convert n-dimensional arrays

##### Share on other sites

QUOTE (crelf @ Apr 22 2009, 06:24 PM)

You mean "created an array with the appropriate number of elements outside a for loop, then replaced it's values", right?

In modern versions* of LV that is a waste of time. The for loop does that "for" you behind the scenes. Yes I have tested it. No, I don't have the benchmarks handy to prove it.

Ben

* I think this came in at version 6 with all of the "optimization" that came with that release.

##### Share on other sites

QUOTE (crelf @ Apr 22 2009, 06:24 PM)

QUOTE (neBulus @ Apr 23 2009, 06:45 AM)

In modern versions* of
LV
that is a waste of time. The for loop does that "for" you behind the scenes.

In either case, I would leave it up to the developer to decide whether the data structure is large enough to necessitate that. If you keep giving out complete answers like that, crelf, you might find yourself out of work ...

##### Share on other sites

QUOTE (crelf @ Apr 23 2009, 10:13 AM)

There's no way for the For Loop to know how many elements are going to be in the array...

If the loop is indexed, there sure is a way for the loop to know. I'm sure I've read an app note for 7.1 that concurs with Ben's statement. Trying to find it now.

EDIT:

Here we go - app note 168:

QUOTE

If you want to add a value to the array with every iteration of the loop, you can see the best performance by using

auto-indexing on the edge of a loop. With For Loops, the VI can predetermine the size of the array (based on the value

wired to N), and resize the buffer only once.

I don't know if something under the hood in LV has changed since then that makes the preallocate and replace faster.

##### Share on other sites

QUOTE (asbo @ Apr 23 2009, 09:18 AM)

If you keep giving out complete answers like that, crelf, you might find yourself out of work ...

It's all about tapping into the limitless power of communiity:

##### Share on other sites

QUOTE (Gary Rubin @ Apr 23 2009, 10:22 AM)

If the loop is indexed, there sure is a way for the loop to know. I'm sure I've read an app note for 7.1 that concurs with Ben's statement. Trying to find it now.

EDIT:

Here we go - app note 168:

I don't know if something under the hood in LV has changed since then that makes the preallocate and replace faster.

Thank you Gary!

Since we have wondered to the topic of performance I'll share my list of code construct choice from fastest to slowest where in/out arrays are the same size.

LV Primative - Like the Multiply Array.

For Loop - LV pre-allocates and replaces

While Loop with Pre-allocated array - Fater because of no need to re-allocate more momory.

While Loop no pre-allocate

I am not usre about the For Loop with condition terminal but I'd gues it fits in after the For Loop but before the While Loop with pre-allocated buffer.

Ben

##### Share on other sites

QUOTE (Gary Rubin @ Apr 23 2009, 10:22 AM)

If the loop is indexed, there sure is a way for the loop to know preallocate and replace faster.

<trying not be feel offended> Of course that's true when talking about about an array that is indexed by the loop </trying not be feel offended>, but my understanding was that we weren't talking about that - we were talking about an array where the size is not defined by the number of loop executions.

##### Share on other sites

QUOTE (crelf @ Apr 23 2009, 11:10 AM)

<trying not be feel offended> Of course that's true when talking about about an array that is indexed by the loop </trying not be feel offended>, but my understanding was that we weren't talking about that - we were talking about an array where the size is not defined by the number of loop executions.

Ah, good point.

##### Share on other sites

Thanks you guys, you knocked the question out of the park.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×