"Projecting" two dimensions onto one?


#1

Hi,

is there a way to “project” two dimensions onto one?

I’ve got a 2-dimensional array that looks like this:
{x,y} value
{1,1} 1
{1,2} 2
{1,3} 3
{2,1} 4
{2,2} 5
{2,3} 6
{2,4} 7
{3,1} 8
{4,1} 9
{5,1} 10
{5,2} 11
{6,1} 12
{6,2} 13
{6,3} 14
{6,4} 15

And ideally I want to end up with a 1-dimensional array that looks something like this:
{z} value
{1} 1
{2} 2
{3} 3
{4} 4
{5} 5
{6} 6
{7} 7
{8} 8
{9} 9
{10} 10
{11} 11
{12} 12
{13} 13
{14} 14
{15} 15

but any 1-dimensional projection that keeps the order of the values would do (even if there are empty cells between the “rows”).

I’m having trouble achieving this with reshape() - I guess because I would like the y dimension to be unbounded…


#2

Will unpack operator work for you? It turns a multi-dimensional arrays into flat vectors. You might need to use project with unpack.


#3

Thanks, I’ll try that :smile:

Meanwhile I found out that redimension() seems to do the trick perfectly, even for unbounded arrays :smile:


#4

Tried it; to summarize:

unpack returns this for my array:
{0} 1,1,1
{1} 1,2,2
{2} 1,3,3
{3} 2,1,4
{4} 2,2,5
{5} 2,3,6
{6} 2,4,7
{7} 3,1,8
{8} 4,1,9
{9} 5,1,10
{10} 5,2,11
{11} 6,1,12
{12} 6,2,13
{13} 6,3,14
{14} 6,4,15

while redimension does this:
{0} 1
{1} 2
{2} 3
{3} 4
{4} 5
{5} 6
{6} 7
{7} 8
{8} 9
{9} 10
{10} 11
{11} 12
{12} 13
{13} 14
{14} 15

It seems redimension also works nicely when projecting from 4-dim to 2-dim. I’ll have to play around with larger arrays to see if there are speed differences.