我被问到以下问题:
给定表示2D矩阵的1D阵列,以0(n)或更好的方式垂直翻转值 . 给出矩阵的高度和宽度 .
我这样做的方式如下:
// Given width and height
int w = 8;
int h = 2;
// Initialize the arrays
float[] random = new float[w*h];
float[] output = new float[w*h];
// Fill with random values
for(int i = 0; i < random.length; i++) {
//random[i] = (float)(Math.random());
random[i] = (float)(i);
}
// Flip code
int flipped = 0;
for(int i = 0; i < w; i++) {
for(int j = 0; j < h; j++){
flipped = Math.abs(i-(w-1));
output[j*w+i] = random[j*w+flipped];
}
}
后续问题是,如果它代表一个3D矩阵,如何在0(n)或更好的情况下翻转?
这个例子是为了澄清:
Width = 2
Height = 2
Depth = 4
Given = [0000111122223333]
Represents = [ [[0000],[1111]] , [[2222],[3333]] ]
Flipped = [1111000033332222]
我现在还知道怎么做?