Search code examples
javascriptarraysmathmatrixscaling

Horizontal 2d matrix transformation (scale) - JAVASCRIPT


I have a 2d matrix like this example below:

00000000000000000000000000000000000000000000000000
00110000011000111111000110000000110000001111100000
00110000011000110000000110000000110000010000010000
00110000011000110000000110000000110000010000010000
00111111111000111111000110000000110000010000010000
00110000011000110000000110000000110000010000010000
00110000011000110000000110000000110000010000010000
00110000011000111111000111111000111111001111100000
00000000000000000000000000000000000000000000000000

and I'd like to transform this matrix by scaling the matrix horizontally with a specific factor (e.g 2.0 or 0.5, ...) so that the matrix afterwards looks like this:

000000000000000000000000000000000000000000000000000000000000000000000000000000 ...
00111100000000000111100000011111111111111000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111111111111111111100000011111111111111000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111100000000000111100000011110000000000000000111100000000000000 ...
00111100000000000111100000011111111111111000000111111111111110000 ...
0000000000000000000000000000000000000000000000000000000000000000000000000000000 ...

or

00000000000000000000000000000000000
00100010011110010000010000001110000
00100010010000010000010000010001000
00100010010000010000010000010001000
00111110011110010000010000010001000
00100010010000010000010000010001000
00100010010000010000010000010001000
00100010011110011110011110001110000
00000000000000000000000000000000000

My javascript "matrix" -array- looks like this:

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;
}

So I would be super thankful about some code ideas because I have no clue how to continue coding this function.

Thanks a lot in advance, jonas


Solution

  • You can put it all into one function:

    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;
    }
    

    Example output:

    > scaleHorizontal(matrix, 0.25)
    ["0000000000000", 
     "0000101010110", 
     "0000001010000", 
     "0000001010000", 
     "0110101010000", 
     "0000001010000", 
     "0000001010000", 
     "0000101111110", 
     "0000000000000"]