You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/datatypes/matrices.md
+37-5Lines changed: 37 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -270,12 +270,12 @@ in the matrix, and if not, a subset of the matrix will be returned.
270
270
271
271
A subset can be defined using an `Index`. An `Index` contains a single value
272
272
or a set of values for each dimension of a matrix. An `Index` can be
273
-
created using the function `index`. When getting a single value from a matrix,
274
-
`subset` will return the value itself instead of a matrix containing just this
275
-
value.
273
+
created using the function `index`. The way `subset` returns results depends on how you specify indices for each dimension:
276
274
277
-
**New behavior:**
278
-
When using `subset`, indexing a dimension with a scalar removes that dimension from the result, while indexing with an array (even if it has only one element) preserves that dimension. In other words, scalar indices eliminate dimensions, and array indices retain them. To restore the previous behavior, where retrieving an index with an Array or Matrix with size 1 or a single value always returns the value itself regardless of index type, set the configuration option `{legacySubset: true}` using the `config` function.
275
+
- If you use a scalar (single number) as an index for a dimension, that dimension is removed from the result.
276
+
- If you use an array, matrix or range (even with just one element) as an index, that dimension is preserved in the result.
277
+
278
+
This means that scalar indices eliminate dimensions, while array, matrix or range indices retain them. See the section [Migrate to v15](#migrate-to-v15) for more details and examples of this behavior.
With the release of math.js v15, the behavior of `subset` when indexing matrices and arrays has changed. If your code relies on the previous behavior (where indexing with an array or matrix of size 1 would always return the value itself), you may need to update your code or enable legacy mode.
339
+
340
+
### How to migrate
341
+
342
+
-**Option 1: Enable legacy behavior**
343
+
If you want your code to work as before without changes, enable legacy mode by adding:
344
+
```js
345
+
math.config({ legacySubset:true })
346
+
```
347
+
This restores the old behavior for `subset`.
348
+
349
+
-**Option 2: Update your code**
350
+
Update your code to use scalar indices when you want to eliminate dimensions, or use array indices to preserve dimensions.
351
+
352
+
### Migration examples
353
+
354
+
```js
355
+
constm=math.matrix([[10, 11, 12], [20, 21, 22]])
356
+
```
357
+
358
+
| Code example | v15+ (default) result | v14- and v15+ (legacy mode) result | How to get old result in v15+. |
0 commit comments