I want to flatten a 2d (n x n) matrix in python into a 1d array, but instead of row major order, I want it to follow the ordering of hilbert curve?
For example, if my input data is 2x2 -->
data[[a b] [c d]]
I want the output to be 1x4 -->
[c, a, b, d]
but I want to do this with an image of say size 256 x 256
Another example is given data
[[12 15 5 0]
[ 3 11 3 7]
[ 9 3 5 2]
[ 4 7 6 8]]
I want the output to be
[ 4 7 3 9 3 12 15 11 3 5 0 7 2 5 6 8]
What is the best way to do this in python?
I suggest you to use hilbertcurve library.
First of all your matrix size must be 2^n. If you want to transform 4x4 matrix (size=16) you need to specify your number of dimensions(N) and number of iterations used in constructing the Hilbert curve(p)
And you get your result
And your curve look like this:
Hilbert cureve
you can play with p value to get different curves, but I think you get the idea.