我有一个2d矩阵,如下例所示:
00000000000000000000000000000000000000000000000000
00110000011000111111000110000000110000001111100000
00110000011000110000000110000000110000010000010000
00110000011000110000000110000000110000010000010000
00111111111000111111000110000000110000010000010000
00110000011000110000000110000000110000010000010000
00110000011000110000000110000000110000010000010000
00110000011000111111000111111000111111001111100000
00000000000000000000000000000000000000000000000000
我希望通过使用特定因子(e.g 2.0 or 0.5, ...)
水平缩放矩阵来转换此矩阵,以便之后的矩阵如下所示:
000000000000000000000000000000000000000000000000000000000000000000000000000000 ...
00111100000000000111100000011111111111111000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111111111111111111100000011111111111111000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111100000000000111100000011111111111111000000111111111111110000 ...
0000000000000000000000000000000000000000000000000000000000000000000000000000000 ...
或
00000000000000000000000000000000000
00100010011110010000010000001110000
00100010010000010000010000010001000
00100010010000010000010000010001000
00111110011110010000010000010001000
00100010010000010000010000010001000
00100010010000010000010000010001000
00100010011110011110011110001110000
00000000000000000000000000000000000
我的javascript“矩阵” - array
- 看起来像这样:
var matrix = [
'001000100001',
'001111101100',
'001111101100',
'001000100001'
]
var matrix = [
'00000000000000000000000000000000000000000000000000',
'00110000011000111111000110000000110000001111100000',
'00110000011000110000000110000000110000010000010000',
'00110000011000110000000110000000110000010000010000',
'00111111111000111111000110000000110000010000010000',
'00110000011000110000000110000000110000010000010000',
'00110000011000110000000110000000110000010000010000',
'00110000011000111111000111111000111111001111100000',
'00000000000000000000000000000000000000000000000000']
Array.prototype.scale_horizontal = function(factor) {
var matrix=this;
var new_matrix=new_matrix(matrix.length, matrix[0].length*factor, 0)
for (var y=0; y<matrix.length; y++) {
for (var x=0; x<matrix[y].length; x++) {
// no idea
}
}
}
function new_matrix(rows, cols, value) {
var arr = [];
for (var i = 0; i < rows; i++) {
arr.push([]);
arr[i].push(new Array(cols));
for (var j = 0; j < cols; j++) arr[i][j] = value;
};return arr;
}
所以我会非常感谢一些代码的想法,因为我不知道如何继续编写这个函数。
非常感谢,jonas
答案 0 :(得分:0)
你可以把它全部放在一个功能中:
function scaleHorizontal(matrix, factor) {
var scaledMatrix = [];
for(var i = 0; i < matrix.length; i++) {
var row = '';
for(var j = 0; j <= matrix[i].length - 1; j += (1 / factor)) {
row += matrix[i][Math.round(j)];
}
scaledMatrix.push(row);
}
return scaledMatrix;
}
示例输出:
> scaleHorizontal(matrix, 0.25)
["0000000000000",
"0000101010110",
"0000001010000",
"0000001010000",
"0110101010000",
"0000001010000",
"0000001010000",
"0000101111110",
"0000000000000"]