Good day to all!
Faced a problem. I’m not very good with mathematics, but nevertheless, it is necessary for the program to select 2 submatrices (subMatrixA, subMatrixB ) from the matrix (w) according to the following example:
// main matrix
final w = [
[0, 0.6, 0.3, 0.3, 0.5, 0.5],
[0.9, 0, 0.7, 0.8, 0.2, 0.5],
[0.2, 0.8, 0, 0.4, 0.6, 0.3],
[0.1, 0.7, 0.6, 0, 0.7, 0.2],
[0.9, 0.8, 0.5, 0.3, 0, 0.7],
[0.8, 0.6, 0.3, 0.7, 0.7, 0],
];
// a - array of indexes in w matrix (rows and columns)
function selection(a) {...} // need an algorithm
// call function with algorithm
selection([0, 1]) // which means - 0 and 1 line
// result
subMatrixA =
[
[0, 0.6]
[0.9, 0 ]
]
subMatrixB =
[
[0.3, 0.3, 0.5, 0.5]
[0.7, 0.8, 0.2, 0.5]
]
How I can do it? I hope for your help, thanks.
I tried to write code an algorithm, but i very bad in algorithms and math...
CodePudding user response:
(you may need to remove : number[]
types here, im writing in typescript)
// main matrix
const w = [
[0, 0.6, 0.3, 0.3, 0.5, 0.5],
[0.9, 0, 0.7, 0.8, 0.2, 0.5],
[0.2, 0.8, 0, 0.4, 0.6, 0.3],
[0.1, 0.7, 0.6, 0, 0.7, 0.2],
[0.9, 0.8, 0.5, 0.3, 0, 0.7],
[0.8, 0.6, 0.3, 0.7, 0.7, 0],
];
function selection(
source: number[][],
rowIndexes: number[],
columnIndexes: number[] = rowIndexes,
): number[][] {
return rowIndexes.map(
y => columnIndexes.map(
x => source[y][x]
)
);
}
let A = selection(w, [0, 1])
// ^?
console.log(A)
// ^ [[0, 0.6], [0.9, 0]]
function otherIndexes(
indexes: number[],
length: number,
): number[] {
// make [0, 1, 2, ..., length-1] and filter it
return Array.from({ length }, (e, i) => i)
.filter(i => !indexes.includes(i));
}
let B = selection(w, [0, 1], otherIndexes([0, 1], w[0].length))
console.log(B)
// ^ [[0.3, 0.3, 0.5, 0.5], [0.7, 0.8, 0.2, 0.5]]
CodePudding user response:
If I understand your requirements correctly, then it's just a matter of plucking out he required rows then slicing each resulting row based on the count of the rows we're seeking.
const selection = (matrix, rows, count = rows .length) => [
rows .map (r => matrix [r]) .map (r => r .slice (0, count)),
rows .map (r => matrix [r]) .map (r => r .slice (count)),
]
const w = [[0, 0.6, 0.3, 0.3, 0.5, 0.5], [0.9, 0, 0.7, 0.8, 0.2, 0.5], [0.2, 0.8, 0, 0.4, 0.6, 0.3], [0.1, 0.7, 0.6, 0, 0.7, 0.2], [0.9, 0.8, 0.5, 0.3, 0, 0.7], [0.8, 0.6, 0.3, 0.7, 0.7, 0]]
const [matrixA, matrixB] = selection (w, [0, 1])
console .log ('matrixA: ', matrixA)
console .log ('matrixB: ', matrixB)
.as-console-wrapper {max-height: 100% !important; top: 0}
This could be made slightly more efficient by calling row .map (...)
only once. It's not a difficult change, but it will make the code uglier.
There is no error-checking; if you supply rows out of range, anything might happen. Again, this is not hard to fix, but I leave it as an exercise for the reader.