Jump to content

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,

Brad

Link to comment

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,

Brad

Use re-shape array?

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

Ben

Link to comment

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.

Link to comment

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?

Link to comment

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. :shifty:

Ben

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

Link to comment

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 ...

Link to comment

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.

Link to comment

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

Link to comment

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.

Link to comment

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.

Link to comment

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.