06-09-2022 04:06 PM - edited 06-09-2022 04:42 PM
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
06-09-2022 04:37 PM
Hi!
How does this look?
06-09-2022 04:45 PM
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.
06-09-2022 04:52 PM
@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.
06-09-2022 05:01 PM
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.)"
06-09-2022 08:19 PM
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.
06-09-2022 08:20 PM
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
06-09-2022 08:48 PM
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.
06-09-2022 08:54 PM
Thanks to all for the suggestions! It's great to see that the community is still strong and supportive.
06-09-2022 08:56 PM
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: