Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide concatenate along other axes #104

Open
RReverser opened this issue Oct 15, 2021 · 1 comment
Open

Provide concatenate along other axes #104

RReverser opened this issue Oct 15, 2021 · 1 comment

Comments

@RReverser
Copy link

While README mentions that it's possible to achieve concatenation along various axes by using transposition, it's pretty hard to wrap my head around how to do that on multi-dimensional arrays. It would be a lot easier if such concatenation was part of the user-facing API, like it is in numpy.

Example code that I'm trying to convert right now:

        image = np.concatenate((image, image[(h - dh) :, :, :]), axis = 0)
        image = np.concatenate((image, image[:, (w - dw) :, :]), axis = 1)
        
        image = np.concatenate((image, image[(h - offset) :, :, :]), axis = 0)
        image = np.concatenate((image[: offset, :, :], image), axis = 0)
        image = np.concatenate((image, image[:, (w - offset) :, :]), axis = 1)
        image = np.concatenate((image[:, : offset, :], image), axis = 1)

Even if I manage to rewrite it using transpositions, I feel it won't be very readable / maintainable going forward.

@RReverser
Copy link
Author

This is what I came up with for now as a helper that seems to work:

function concatenateAlong(axis, ...arrays) {
  let lastAxis = arrays[0].shape.length - 1;
  let axes = arrays[0].shape.map((_, i) => {
    // swap the required axis with the last one
    switch (i) {
      case lastAxis:
        return axis;
      case axis:
        return lastAxis;
      default:
        return i;
    }
  });
  arrays = arrays.map(a => a.transpose(...axes));
  let result = numjs.concatenate(...arrays);
  return result.transpose(...axes);
}

Does it look reasonable? Is it perhaps worth adding it to the library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant