MCPcopy
hub / github.com/tensorflow/playground / reduceMatrix

Function reduceMatrix

src/heatmap.ts:210–234  ·  view source on GitHub ↗
(matrix: number[][], factor: number)

Source from the content-addressed store, hash-verified

208} // Close class HeatMap.
209
210export function reduceMatrix(matrix: number[][], factor: number): number[][] {
211 if (matrix.length !== matrix[0].length) {
212 throw new Error("The provided matrix must be a square matrix");
213 }
214 if (matrix.length % factor !== 0) {
215 throw new Error("The width/height of the matrix must be divisible by " +
216 "the reduction factor");
217 }
218 let result: number[][] = new Array(matrix.length / factor);
219 for (let i = 0; i < matrix.length; i += factor) {
220 result[i / factor] = new Array(matrix.length / factor);
221 for (let j = 0; j < matrix.length; j += factor) {
222 let avg = 0;
223 // Sum all the values in the neighborhood.
224 for (let k = 0; k < factor; k++) {
225 for (let l = 0; l < factor; l++) {
226 avg += matrix[i + k][j + l];
227 }
228 }
229 avg /= (factor * factor);
230 result[i / factor][j / factor] = avg;
231 }
232 }
233 return result;
234}

Callers 1

updateUIFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…