如何在ND4J中反转矩阵的列元素?

huangapple go评论62阅读模式
英文:

how can I reverse the elements of a column of a matrix in ND4J?

问题

我正尝试使用ND4j库将Python代码行转写为JAVA代码:

data.getRow(index_odd).assign(tmp.get(NDArrayIndex.all(), NDArrayIndex.interval(tmp.columns() - 1, -1, 0)));

它会反转列的元素(换句话说,行的顺序)。我使用了以下代码,但显然仅在end > begin时有效:

INDArrayIndex rev_idx = NDArrayIndex.interval(10, 1);

提前感谢您的帮助。

英文:

I am trying to rewrite this line of python code in JAVA with ND4j library.

data[index_odd] = tmp[::-1,:]

It reverses the column's elements. (in other words, the order of rows)
I used this line but apparently it just works when end>begin

INDArrayIndex rev_idx = NDArrayIndex.interval(10, 1);

I appreciate your help in advance

答案1

得分: 0

我用一种非常巧妙的方式解决了我的问题:))
我打算与你分享。然而,如果你能提供一个健壮的答案,我将不胜感激。

    int[] idx = new int[(int) OBJ_imag.shape()[0]];
    for(int i = 0 ; i < OBJ_imag.shape()[0] ; i ++)
    	idx[i] = (int) (OBJ_imag.shape()[0] - i - 1);
    imag.put(indx, OBJ_imag.getRows(idx));
英文:

I solved my problem in a very nasty way :))
I am going to share it with you. However, if you come with a robust answer, I will appreciate it.

int[] idx = new int[(int) OBJ_imag.shape()[0]];
for(int i = 0 ; i&lt;OBJ_imag.shape()[0] ; i ++)
	idx[i] = (int) (OBJ_imag.shape()[0] - i -1);
imag.put(indx, OBJ_imag.getRows(idx));

huangapple
  • 本文由 发表于 2020年8月19日 17:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63484033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定