0

i am trying to permute an array as described in the documentation (actual a link to mathworks).

My code, however does not reproduce the results. Actually, it seems like my code does nothing:

INDArray foobar = Nd4j.linspace(0, 2, 3);
System.out.println("foobar = " + foobar);
System.out.println("foobar.permute(2, 1, 0) = " + foobar.permute(2, 1, 0));

Output is the same for foobar and foobar.permute. Where did I mess up?

Optional: Any information on how permute work (syntax-wise) with higher dimensions like 2d or 3d matrices would be appreciated.

SiOx
  • 478
  • 1
  • 6
  • 13

2 Answers2

1

According to the documentation you linked to, permute method is used to permute the dimensions of a multi-dimensional array. Nd4j.linspace creates a vector (a 1-dimensional array). Since there is only one dimension, there is no other dimension to permute it with. The example on Mathworks on the other hand, creates a 3-dimentional array and permutes the 1st and 3rd dimension.

It sounds like your intention was to permute the individual elements of an array (not entire dimensions). I haven't seen a method for that in Nd4j.

Michal Borowiecki
  • 4,244
  • 1
  • 11
  • 18
0

I had the same problem. I don't think there is a method inside the Nd4j. I wrote a method to reorder an ND4j vector.

publin INDArray reorder(int[] index) {
        INDArray copy = q.dup();
        for (int i=0; i<copy.length(); i++){
            copy.put(i, q.getScalar(index[i]));
        }
        return copy;
}