# mat.matrixMap()

{% tabs %}
{% tab title="💾 程式" %}

```javascript
const matrix_map = {
    /**
     * map a matrix (to become another matrix)
     * @param {function} f with parameters (value, i, j)
     * @example
     * const m = [[1, 2], [4, 5]];
     * m.matrixMap(x => 2 * x)    // [[2,4],[8,10]]
     * m.matrixMap((x, i, j) => i === j ? 1 : 0)  // [[1,0],[0,1]]
     */
    matrixMap(f) {
        return this.map((row, i) => row.map((value, j) => f(value, i, j)))
    }
};

export default matrix_map;
```

💈範例：

```javascript
import matrix_map from '../matrix_map.mjs';
Object.assign(Array.prototype, matrix_map);

const m = [[1, 2], [4, 5]]
m.matrixMap(x => 2 * x)                    // [ [ 2, 4 ], [ 8, 10 ] ]
m.matrixMap((x, i, j) => i === j ? 1 : 0)  // [ [ 1, 0 ], [ 0, 1 ] ]
```

{% endtab %}

{% tab title="📘 手冊" %}

* [Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/builtin/arr/matrix-methods/mat.matrixmap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
