Skip to content
Advertisement

Loading multiple 2d arrays with different shapes into a new array on a third dimension

I’m currently struggling with a probably rather simple question but I can’t get my head around it.

Assuming I have the follow two 2d arrays with different shapes, I can combine them into a new array using:

JavaScript

So far so good! But how would I do this if I have a large list where I’d have to iterate over? Here is a simple example with just 4 different 2d arrays:

This works as expected:

JavaScript

This doesn’t work as expected and my question would be how to do this in an iterative way:

JavaScript

I understand that in each iteration I’m just combining two arrays why it always stays at shape of (2,), but I wonder how this can be done in an elegant way. So basically I want to have a third dimension which states the count or amount of arrays that are stored. E.g. if I iterate of 1000 different 2d arrays I’d expect to have a shape of (1000,)

Hope my question is understandable – if not let me know! Thanks a lot!

Advertisement

Answer

If I understood your issue correctly, you can achieve what you want in a list comprehension. This will yield the exact same solution as your code above that you described as working.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement