Java Matrix Flatten flattenIndicesCollection(int[][] multipleIndices, int[] extents)

Here you can find the source of flattenIndicesCollection(int[][] multipleIndices, int[] extents)

Description

Given a multiple n-d indices flatten them to a collection of 1d indices

License

Open Source License

Parameter

Parameter Description
multipleIndices multiple n-d indices
extents the maximum size in each dimension

Return

the collection of 1d indices

Declaration

public static int[] flattenIndicesCollection(int[][] multipleIndices,
        int[] extents) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* www . j  a v  a 2s.co m*/
     * Given a multiple n-d indices flatten them to a collection of 1d indices
     *
     * @param multipleIndices multiple n-d indices
     * @param extents         the maximum size in each dimension
     * @return the collection of 1d indices
     */
    public static int[] flattenIndicesCollection(int[][] multipleIndices,
            int[] extents) {
        int[] flattened = new int[multipleIndices.length];
        for (int i = 0; i < multipleIndices.length; i++) {
            flattened[i] = flattenIndices(multipleIndices[i], extents);
        }
        return flattened;
    }

    /**
     * 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;
    }
}

Related

  1. Flatten(int[][] in, int[] out)
  2. flatten(String[][] table)
  3. flatten2DArray(byte[][] array)
  4. flatten2DArray(double[][] array)
  5. flatten4(float[][] in, int size, float[] out)
  6. flattenN(float[][] in, int size, int width, float[] out)