mat.maxElementInEachColumn()

⬆️ 需要: mat.transpose()

// ⬆️ 需要: matrix.transpose()
import matrix_transpose from '../mixin/matrix_transpose.mjs';
Object.assign(Array.prototype, matrix_transpose);

/**
 * find max element in each column
 * @example
 * import matrix_maxElementInEachColumn from './matrix_maxElementInEachColumn.mjs';
 * Object.assign(Array.prototype, matrix_maxElementInEachColumn);
 */
const matrix_maxElementInEachColumn = {

    /**
     * find max element in each column
     * @example
     * let m = [[1, 5, 2],[2, 6],[3, 4, 5]]
     * m.maxElementInEachColumn()  // [ 3, 6, 5 ]
     * @return {[number]}
     */
    maxElementInEachColumn() {
        return this
            .transpose()                                        // see matrix in cols
            .map(col => Math.max(...col.removeUndefined()))     // max element in each col
    },

};

export default matrix_maxElementInEachColumn;

Last updated