# 多維矩陣轉一維矩陣

## 解法

loc = column + row*行數

loc = row + column*列數

## 實作：CJavaPythonScala Ruby

• C
``#include <stdio.h> #include <stdlib.h>#define ROW 3#define COLUMN 4void toOneByRow(int[][], int[]);void toOneByColumn(int[][], int[]);int indexByRow(int, int);int indexByColumn(int, int);void toOne(int[][COLUMN], int[], int (*)(int, int));int main(void) {     int arr1[ROW][COLUMN] = {{1, 2, 3, 4},                              {5, 6, 7, 8},                              {9, 10, 11, 12}};     int arr2[ROW * COLUMN] = {0};     printf("原二維資料：\n");     int row, column;    for(row = 0; row < 3; row++) {         for(column = 0; column < 4; column++) {             printf("%4d", arr1[row][column]);         }         printf("\n");     }     printf("\n以列為主：");     toOneByRow(arr1, arr2);    int i;    for(i = 0; i < 12; i++)         printf("%d ", arr2[i]);     printf("\n以行為主：");     toOneByColumn(arr1, arr2);    for(i = 0; i < 12; i++)         printf("%d ", arr2[i]);     printf("\n");     return 0; } void toOneByRow(int arr1[][COLUMN], int arr2[]) {    toOne(arr1, arr2, indexByRow);}void toOneByColumn(int arr1[][COLUMN], int arr2[]) {    toOne(arr1, arr2, indexByColumn);}int indexByRow(int row, int column) {    return column + row * COLUMN;}int indexByColumn(int row, int column) {    return row + column * ROW;}void toOne(int arr1[][COLUMN], int arr2[], int (*index)(int, int)) {    int row, column;    for(row = 0; row < ROW; row++) {         for(column = 0; column < COLUMN; column++) {             arr2[index(row, column)] = arr1[row][column];         }     }    }``

• Java
``public class Matrix {       public static int[] toOneByRow(final int[][] array) {        return toOne(array, new Index() {            public int get(int row, int column) {                return column + row * array[0].length;             }        });    }        public static int[] toOneByColumn(final int[][] array) {        return toOne(array, new Index() {            public int get(int row, int column) {                return row + column * array.length;             }        });    }    private static interface Index {        int get(int row, int column);    }        private static int[] toOne(int[][] array, Index index) {        int[] arr = new int[array.length * array[0].length];        for(int row = 0; row < array.length; row++) {             for(int column = 0; column < array[0].length; column++) {                 arr[index.get(row, column)] = array[row][column];             }         }        return arr;    }}``

• Python
``def toOneByRow(array):    return toOne(array, (lambda row, column: column + row * len(array[0])))def toOneByColumn(array):    return toOne(array, (lambda row, column: row + column * len(array)))def toOne(array, f):    arr = [0] * (len(array) * len(array[0]))    for row in range(len(array)):        for column in range(len(array[0])):            arr[f(row, column)] = array[row][column]    return arr``

• Scala
``object Matrix {    def toOneByRow(array: Array[Array[Int]]) = {        toOne(array, _ * array(0).length + _)    }        def toOneByColumn(array: Array[Array[Int]]) = {        toOne(array, _ + _ * array.length)    }        private def toOne(array: Array[Array[Int]], f: (Int, Int) => Int) = {        val arr = new Array[Int](array.length * array(0).length)        for(row <- 0 until array.length; column <- 0 until array(0).length) {            arr(f(row, column)) = array(row)(column)        }        arr        }}``

• Ruby
``def toOneByRow(array)    toOne(array) { |row, column| column + row * array[0].length }enddef toOneByColumn(array)    toOne(array) { |row, column| row + column * array.length }enddef toOne(array)    arr = Array.new(array.length * array[0].length, 0)    array.length.times { |row|        array[0].length.times { |column|            arr[yield(row, column)] = array[row][column]        }    }    arrend``