# 多維矩陣降維

December 12, 2021

## 解法思路

loc = column + row * 行數

loc = row + column * 列數

## 程式實作

``````#include <stdio.h>
#include <stdlib.h>
#define ROW 3
#define COLUMN 4

void 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];
}
}
}
``````
``````public class Matrix {
public static int[] toOneByRow(final int[][] array) {
public int get(int row, int column) {
return column + row * array[0].length;
}
});
}

public static int[] toOneByColumn(final int[][] array) {
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;
}
}
``````
``````def toOneByRow(array):

def toOneByColumn(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
``````
``````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
}
}
``````
``````def toOneByRow(array)
toOne(array) { |row, column| column + row * array[0].length }
end

def toOneByColumn(array)
toOne(array) { |row, column| row + column * array.length }
end

def 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]
}
}
arr
end
``````
``````function toOneByRow(array) {
}

function toOneByColumn(array) {
}

function toOne(array, f) {
arr = []
for(let row = 0; row < array.length; row++) {
for(let column = 0; column < array[0].length; column++) {
arr[f(row, column)] = array[row][column];
}
}
return arr
}

let array = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
];

console.log(toOneByRow(array));
console.log(toOneByColumn(array));
``````
``````import Data.List

toOneByRow matrix = [elem | row <- matrix, elem <- row]

toOneByColumn matrix = [elem | row <- transpose matrix, elem <- row]

main =
let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
in do
print \$ toOneByRow matrix
print \$ toOneByColumn matrix
``````