Issue
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…
Solution
(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]]
Answered By – Dimava
Answer Checked By – Willingham (FlutterFixes Volunteer)