0

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...

Mahoro
  • 3
  • 2
  • 1
    Does this answer your question? [Slice/Section of two dimensional array in JavaScript](https://stackoverflow.com/questions/51383031/slice-section-of-two-dimensional-array-in-javascript) – SomeDude Nov 08 '22 at 11:58
  • @SomeDude , no, this answer doesn't fit my situation... – Mahoro Nov 08 '22 at 12:19
  • a) can you use NPM library? b) can you have skipped rows (like `selection([1, 4])`)? c) A has same rows# as cols#, and B has same rows and leftover cols? @Mahoro – Dimava Nov 08 '22 at 15:11
  • @Dimava, Preferably with a pure js algorithm. Well, or with libraries on dart (I am writing a program on flutter). In general, there, according to the idea, it is necessary so that it is possible to select the rows of the matrix and 2 submatrices consist only of the selected rows. MatrixA - will always be square, and MatrixB - will always consist of the remainder of that (the remainder of the rows) that was not included in MatrixA. – Mahoro Nov 08 '22 at 15:18

2 Answers2

0

Playground

(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]] 

Dimava
  • 7,654
  • 1
  • 9
  • 24
0

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.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103