Redimension of an a one dimension array with n attributes to two dimensions


If I have a 1-dimensional array with the following schema

create array features <instance_id:int64, feature 1:double, feature 2:double, … feature 10:double>[i=0:500]

would it be possible to redimension it to a 2-dimensional array where the first dimension would be the instance_id, the second dimension would be the index of the feature, i.e. 1, 2, 3 etc, and the cell value would store the double value of each feature?

I’m trying to do something like the following

store(redimension(features, val:double[instance_id=0:500; feature_id=0:9]), features_2d)

but I do not know how to specify that val takes the value of each of feature1, feature2, etc.

The reason I’d like to do this is that I’d like to filter entire instances based on an attribute in the initial 1-dimensional array and then convert it to a 2-dimensional in order to perform matrix multiplication with another array.

Thank you for your time.

I think the following will work for you, where you fully-specify the attributes in the ellipses from my example:

cast(unfold(redimension(features, <feature1:double,...>[instance_id])), <feature:double>[instance_id, feature_index])

This works by reshaping the input features array into one where the dimension takes its values from the instance_id of the input, while preserving the existing attributes. Next, unfold transforms the attributes into a new dimension (which becomes the second dimension for your matrix). Finally, cast simply renames the dimensions and attributes but is not strictly required.

Let me know if this works for you!

Hello Dave,

thank you very much for your reply!
That indeed does the trick in bringing the array to the desired format, but when I try to use the array created by the cast in a subsequent matrix multiplication, I’m getting the following error:

Error during schema inferring. Unbounded arrays not supported.

Using function show, I can see that the dimensions of instance_id and feature_index are 0:*.

Is there a workaround for this?


Hi Nantia,

Yes, after you store the array produced by the cast(unfold... to some array B, you can use scan to trim the unbounded array to a bounded one by providing it the array B and a second boolean parameter true:

Array C should be the bounded matrix that you seek.


Hello Dave,

I’m sorry for my late reply. Thank you very much for your answer. It is indeed bounded now.

No problem, I’m glad that it worked! :grinning: