LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to efficiently concatenate 2D arrays

I need to concatenate two 2D arrays efficiently. (E.g. this will be on an embedded system in a fast-ish loop.) Each array is 20 rows by 20 columns. The rows are channels and the columns are points in time. The default concatenate adds the new array as more rows, whereas I want to add them as more columns. (E.g. each channel is getting more points over time, and I need to build up the waveforms.) So the easy solution is to transpose the two arrays, concatenate, then transpose back, as in the image. Seems unnecessarily intense.

I suppose one alternative would be to pre-allocate the full array, then use Replace Subset to put each sub-array in.

Any other quick, clever ideas?

TIA,

    DaveT

-------------------------------------------------------------
David Thomson Original Code Consulting
www.originalcode.com
National Instruments Alliance Program Member
Certified LabVIEW Architect
Certified Embedded Systems Developer
-------------------------------------------------------------
There are 10 kinds of people: those who understand binary, and those who don't.
0 Kudos
Message 1 of 11
(1,283 Views)

Hi!

 

Xonmyth743_1-1654810617740.png

 

How does this look?

0 Kudos
Message 2 of 11
(1,270 Views)

Nice! Thanks. I'll debate using that vs pre-allocating. Pre-allocating is probably (or at least used to be) more efficient, but your idea allows me more flexibility with changing how many times I concatenate.

-------------------------------------------------------------
David Thomson Original Code Consulting
www.originalcode.com
National Instruments Alliance Program Member
Certified LabVIEW Architect
Certified Embedded Systems Developer
-------------------------------------------------------------
There are 10 kinds of people: those who understand binary, and those who don't.
0 Kudos
Message 3 of 11
(1,265 Views)

@Dave_Thomson wrote:

Nice! Thanks. I'll debate using that vs pre-allocating. Pre-allocating is probably (or at least used to be) more efficient, but your idea allows me more flexibility with changing how many times I concatenate.


I think pre-allocate is the best, but you can use Matrix operations to achieve your result.

 

snip.png

0 Kudos
Message 4 of 11
(1,259 Views)

Okay, I would be curious to know what you ended up using.

 

I focused on "(E.g. each channel is getting more points over time, and I need to build up the waveforms.)"

0 Kudos
Message 5 of 11
(1,241 Views)

Is there any reason why you can't change your definition to "rows are points in time and columns are channels"? If it is just for graphing, there is a transpose option on the graph.

0 Kudos
Message 6 of 11
(1,214 Views)

One more simple method you might add to your benchmarking tests.   Note: uses auto-indexing on both orig and new 2D arrays, so only valid when # rows cannot change throughout the run.

 

 

-Kevin P

 

concat 2d columns.png

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 7 of 11
(1,212 Views)

Pauldavey

>Is there any reason why you can't change your definition to "rows are points in time and columns are channels"? If it is just for graphing, there is a transpose option on the graph.

The data is from a Daq task, so it comes out with the given dimension definition. Switching it would just mean transposing, as per my original image, which is what I'm trying to avoid.

-------------------------------------------------------------
David Thomson Original Code Consulting
www.originalcode.com
National Instruments Alliance Program Member
Certified LabVIEW Architect
Certified Embedded Systems Developer
-------------------------------------------------------------
There are 10 kinds of people: those who understand binary, and those who don't.
0 Kudos
Message 8 of 11
(1,198 Views)

Thanks to all for the suggestions! It's great to see that the community is still strong and supportive.

-------------------------------------------------------------
David Thomson Original Code Consulting
www.originalcode.com
National Instruments Alliance Program Member
Certified LabVIEW Architect
Certified Embedded Systems Developer
-------------------------------------------------------------
There are 10 kinds of people: those who understand binary, and those who don't.
0 Kudos
Message 9 of 11
(1,192 Views)

I suspect that a simple transpose on a relatively small array is going to cost less than all the gymnastics of other techniques. In any case, it is worth considering:

  • How much performance is required - is the current performance adequate
  • Simple code is preferable to complex code that has a minor performance gain (unless it is absolutely necessary).
0 Kudos
Message 10 of 11
(1,190 Views)