[英]How to iterate over indices of a matrix?
在 Python 中,當我們想要遍歷一個任意維度的矩陣時,我們可以使用這行代碼:
for index in np.ndindex(data.shape[2:]):
例如:
> for index in np.ndindex(3, 2, 1):
> print(index) (0, 0, 0) (0, 1, 0) (1, 0, 0) (1, 1, 0) (2, 0, 0) (2, 1, 0)
在java中,一個簡單的方法,我們可以用確定的for循環次數來完成,但前提是要了解維度。 但在任意維度上,算法必然更加復雜。
ND4J 庫中是否有用於迭代索引的內置方法?
在 nd4j 中,我們有一個NDIndexIterator允許您迭代坐標。
這是示例:
NdIndexIterator shapeIter = new NdIndexIterator(2, 2);
//import org.nd4j.linalg.api.iter.NdIndexIterator;
long[][] possibleSolutions = new long[][] {{0, 0}, {0, 1}, {1, 0}, {1, 1},};
for (int i = 0; i < 4; i++) {
assertArrayEquals(possibleSolutions[i], shapeIter.next());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.