Here you can find the source of flattenIndices(int[] indices, int[] extents)
Parameter | Description |
---|---|
indices | n-dimensional indices |
extents | the maximum size in each dimension |
public static int flattenIndices(int[] indices, int[] extents)
//package com.java2s; //License from project: Open Source License public class Main { /**// w w w .ja v a2 s .co m * Given n-dimensional indices and their dimensional extents flatten them into a 1d index. * More detail: http://stackoverflow.com/questions/7367770/how-to-flatten-or-index-3d-array-in-1d-array * * @param indices n-dimensional indices * @param extents the maximum size in each dimension * @return Converts n-dimensional indices into a 1-dimension index */ public static int flattenIndices(int[] indices, int[] extents) { int totalIdx = 0; for (int i = 0; i < indices.length; i++) { int idx = indices[i]; for (int j = 0; j < i; j++) { int extent = extents[j]; idx *= extent; } totalIdx += idx; } return totalIdx; } }