Skip to main content
14-Alexandrite
December 18, 2014
Solved

How best to convert an array with subarrays into an array without subarrays?

  • December 18, 2014
  • 4 replies
  • 2556 views

The attached Prime 3.0 worksheet is a simplified version of what I am trying to do. I am working on a fairly lengthy program function that will accept single-column input arrays (vectors) of varying lengths (or even just a single number) and output an array with several elements. Input/output-wise, it is no different than the simple example in the attached worksheet.

When I use the whole data array as the input, the output is an array with vertical subarrays for the three columns. When I use the data array one element at a time (range variable), the output is an array with horizontal subarrays for the rows. This makes sense to me, but it's not quite what I want. What I really want is a single array without subarrays so I can do other things with the output more easily. I don't see that Prime 3.0 has a simple function to "un-subarray" an array.

I need some ideas on the best / most efficient / most elegent method for producing a single array from an array with subarrays.

Thanks.

Best answer by Werner_E

So here is what I came up with (not that I would think that its any better than what Stuart had provided).

4 replies

Werner_E
25-Diamond I
December 18, 2014

Here is on way to do it but I don't think ist the "most elegant" as I don't like to use the ranges for that purpose.

I'd rather prefer using a program which will take the nested matrix which a vectorized call would return and calculates and returns the unnested matrix.

But as a quick hack this appraoch may suffice:

1.png

EDIT: Just noticed - you can omit the vectorization in that appraoch as you are only passing a signle scalar as argument.

23-Emerald V
December 19, 2014

Fred Lusk wrote:

The attached Prime 3.0 worksheet is a simplified version of what I am trying to do. I am working on a fairly lengthy program function that will accept single-column input arrays (vectors) of varying lengths (or even just a single number) and output an array with several elements. Input/output-wise, it is no different than the simple example in the attached worksheet.

When I use the whole data array as the input, the output is an array with vertical subarrays for the three columns. When I use the data array one element at a time (range variable), the output is an array with horizontal subarrays for the rows. This makes sense to me, but it's not quite what I want. What I really want is a single array without subarrays so I can do other things with the output more easily. I don't see that Prime 3.0 has a simple function to "un-subarray" an array.

I need some ideas on the best / most efficient / most elegent method for producing a single array from an array with subarrays.

Thanks.

I had a function knocking around that did this .. used it for a prototype Multi-Dimensional Array worksheet that attempted to emulate Mathematica's array handling.

However, it doesn't appear to be on my work PC, so I've flung this one together (tacked on to the end of the createspace worksheet just to easily generate some test data).

I seem to recall that the original function handled nested matrices in some fashion as well, but I don't have time to implement it now.

Stuart

Unfortunately, I don't have Prime, but you should be able to import it.

collab - 14 12 19 Flattening Regularly Nested Vectors 01.JPG

FredLusk14-AlexandriteAuthor
14-Alexandrite
December 19, 2014

Thanks to both you. I will give these a whirl.

Werner_E
Werner_E25-Diamond IAnswer
25-Diamond I
December 19, 2014

So here is what I came up with (not that I would think that its any better than what Stuart had provided).