Function subset #

获取或设置矩阵或字符串的子集。

Syntax #

math.subset(value, index)                                // retrieve a subset
math.subset(value, index, replacement [, defaultValue])  // replace a subset

Parameters #

Parameter Type Description
matrix Array | Matrix | string 数组、矩阵或字符串
index Index 对于目标数据的每个维度,指定一个索引或一组索引来获取或设置。subset 使用在每个维度中指定的索引的笛卡尔积。
replacement * 数组、矩阵或标量。如果提供,则用 replacement 替换子集。如果未提供,则返回子集。
defaultValue * 默认值,在矩阵大小调整时填充新条目。如果未提供,math.matrix 元素将保持未定义。默认值:undefined。

Returns #

Type Description
Array | Matrix | string 检索到的子集或更新后的矩阵。

Throws #

Type | Description —- | ———–

Examples #

// get a subset
const d = [[1, 2], [3, 4]]
math.subset(d, math.index(1, 0))             // returns 3
math.subset(d, math.index([0, 1], [1]))        // returns [[2], [4]]
math.subset(d, math.index([false, true], [0])) // returns [[3]]

// replace a subset
const e = []
const f = math.subset(e, math.index(0, [0, 2]), [5, 6])  // f = [[5, 0, 6]]
const g = math.subset(f, math.index(1, 1), 7, 0)         // g = [[5, 0, 6], [0, 7, 0]]
math.subset(g, math.index([false, true], 1), 8)          // returns [[5, 0, 6], [0, 8, 0]]

// get submatrix using ranges
const M = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1, 2, 3], [4, 5, 6]]

另请参阅 #

size, resize, squeeze, index

Fork me on GitHub