快速扩展具有特定因子的数组(1和0的2d矩阵)

时间:2017-12-05 18:57:18

标签: javascript jquery arrays matrix scaling

我有一个数组( 2d matrix of ones and zeros ),如下所示,并希望更改它的大小,因此我开发了一个功能,您也可以在下面找到。< / p>

'011111'               '001111111111'
'000000'               '001111111111'
'000011'     --->      '000000000000'
'000001'               '000000001111' 
                       '000000001111'
                       '000000000011' 
'011111'               '001111111111'
'000000'               '001111111111'
'000011'     <---      '000000000000'
'000001'               '000000001111' 
                       '000000001111'
                       '000000000011' 

因此上面的示例任务可以正常使用以下代码。但是将一个大矩阵 (2000*1000) 缩小到 (40*20) 需要很长时间。另一方面 (40*20) (2000*1000)

  

那么有人知道如何编辑我的代码以使其更快地运行吗?

所以如果有人分享一些与我的问题相符的工作代码,我会非常感激,但是...提前感谢一百万,

尼娜。

&#13;
&#13;
var matrix=[

  '011111000000000000000000001111000000000000000000000000000000000000000011111110000000000000001011',
  '001111000000000000000000011110000000000000000011111000000000000000000001111111000000000000011101',
  '001111100000000000000000111110000000000000111111111111100000000000000000011111100000000000010100',
  '011111000000000000000000111100000000000111111111111111111000000000000000000111110000000000111000',
  '000000000000000000000001111100000000001111111111111111111110000000000000000001100000000000111000',
  '000000000000000000000001111000000000111111111111111111111111000000000000000000000000000001110000',
  '000000000000000000000011110000000001111111111111111111111111100000000000000000000000000001110000',
  '000000000000000000000111110000000011111111111111111111111111111000000000000000000000000011100000',
  '000000000000000000000111100000000111111111111111111111111111111000000000000000000000000111000000',
  '000000000000000000001111100000001111111111111111111111111111111100000000000000000000000111000001',
  '000000000000000000011111000000011111111111111111111111111111111110000000000000000000001110000001',
  '000000000000000000011110000000011111111111111111111111111111111111000000000000000000001110000000',
  '000000000000000000111110000000111111111111111111111111111111111111110000000000000000000111000000',
  '000000000000000000111100000001111111111111111111111111111111111111100000000000000000011000000001',
  '000000000000000001111100000001111111111111111111111111111111111111111000000000000000001110000000',
  '000000000000000011111000000001111111111111111111111111111111111111111000000000000000011100000000',
  '000000000000000011111000000011111111111111111111111111111111111111111110000000000000001111000001',
  '000000000000000111110000000011111111111111111111111111111111111111111110000000000000011100000001',
  '000000000000001111100000000011111111111111111111111111111111111111111100000000000001110000000001',
  '000000000000001111100000000011111111111111111111111111111111111111111110000000000000011100000001',
  '000000000000011111000000000111111111111111111111111111111111111111111110000000000001110000000001',
  '000000000000011111000000000111111111111111111111111111111111111111111110000000000111000000000001',
  '000000000000111110000000000111111111111111111111111111111111111111111110000000000011000000000001',
  '000000000000111100000000000111111111111111111111111111111111111111111110000000000011000000000001',
  '000000000000111100000000000111111111111111111111111111111111111111111110000000111000000000000001',
  '000000000001111000000000000111111111111111111111111111111111111111111110000000010100000000000001',
  '000000000001111000000000000111111111111111111111111111111111111111111100011100000000000000000001',
  '000000000111110000000000000111111111111111111111111111111111111111111000001111000000000000000001',
  '000000000111100000000000000011111111111111111111111111111111111111111000000011100000000000000001',
  '000000000111100000000000000011111111111111111111111111111111111111110000001111000000000000000001',
  '000000000111100000000000000011111111111111111111111111111111111111100000000111000000000000000001',
  '000000101111110000000000000001111111111111111111111111111111111111100000000110000000000000000001',
  '000000001111111000000000000001111111111111111111111111111111111111000000111000000000000000000001',
  '000000000111111110000000000000111111111111111111111111111111111110000000011110000000000000000001',
  '000000000011111111100000000000111111111111111111111111111111111100000000011100000000000000000001',
  '000000000001111111110000000000011111111111111111111111111111111100000000111000000000000000000001',
  '000000000000011111111000000000001111111111111111111111111111111000000001111000000000000000000001',
  '000000000000001111111100000000001111111111111111111111111111110000000001110000000000000000000001',
  '000000000000000011111111000000000011111111111111111111111111100000000001110000000000000000000001',
  '000000000000000001111111110000000001111111111111111111111111000000000010000000000000000000000001',
  '000000000000000000111111111000000000111111111111111111111100000000000111100000000000000000000001',
  '000000000000000000001111111110000000001111111111111111111000000000000111000000000000000000000001',
  '000000000000000000000011111111100000000011111111111111000000000000001111000000000000000000000001',
  '000000000000000000000001111111111000000000001111111000000000000000001110000000000000000000000001',
  '000000000000000000000000011110111100000000000000000000000000000000011100000000000000000000000001'
]


Array.prototype.scale_matrix = function(scale_factor) {
	var matrix = this;
	var scale = 1 / scale_factor;
	var array = duplicate_matrix(Math.round(this.length * scale_factor), Math.round(this[0].length * scale_factor), '0').map(str => Array.from(str, String))
	if (scale_factor < 1) {
		for (var y = 0; y < array.length; y++) {
			for (var x = 0; x < array[0].length; x++) {
				_y = Math.round(y / scale_factor);
				_x = Math.round(x / scale_factor);
				var old_values = []
				for (var oldx = _x; oldx < _x + scale; oldx++) {
					for (var oldy = _y; oldy < _y + scale; oldy++) {
						try {
							old_values.push(matrix[oldy][oldx]);
						}
						catch (e) {}
					}
				}
				var new_value = old_values.sort((a, b) => old_values.filter(v => v === a).length - old_values.filter(v => v === b).length).pop();
				array[y][x] = matrix[_y][_x];
			}
		}
	}
	else {
		for (var y = 0; y < matrix.length; y++) {
			for (var x = 0; x < matrix[0].length; x++) {
				_y = Math.round(y * scale_factor);
				_x = Math.round(x * scale_factor);
				for (var newx = _x; newx < _x + scale_factor; newx++) {
					for (var newy = _y; newy < _y + scale_factor; newy++) {
						try {
							array[newy][newx] = matrix[y][x];
						}
						catch (e) {}
					}
				}
			}
		}
	}
	return array.map(a => [a.join('')]).concat.apply([], array.map(a => [a.join('')]));
}
function duplicate_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;
}


console.log(matrix.scale_matrix(0.3).join('\n'));
console.log('\n\n>------<\n\n');
console.log(matrix.scale_matrix(1.3).join('\n'));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

0 个答案:

没有答案