Skip to content

compute.ndarray.argmin

Summary

compute.ndarray.argmin finds the index of the smallest real value. Use compute.ndarray.argmax for the largest. Both can select along a single axis or across the whole array.

Syntax

argmin<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayArgReduceOptions): T
argmax<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayArgReduceOptions): T
const indices = nd.argmin(a, { axis: 1 });

Parameters

Name Type Required Description
a CPUndarray | GPUndarray Yes Live real-valued input.
options NdarrayArgReduceOptions No Select an axis, NaN handling or GPU output and encoder.

Returns

T - Packed u32 indices in the input residency. With an axis, each index is relative to that axis. With the axis omitted, the result is an index into logical C-order traversal, regardless of the underlying strides. Ties, including signed-zero ties, choose the first logical index.

Type Details

type NdarrayArgReduceOptions = NdarrayOperationOptions & {
    axis?: number;
    keepDims?: boolean;
    nan?: NdarrayNaNMode;
};

Negative axis indices count from the end. keepDims defaults to false. If true, the reduced axes remain at length one. With the default nan: "propagate", the first NaN wins. Using "omit" skips NaNs and returns 0xffffffff for an all-NaN slice. A zero-element slice throws if an output value is required. GPU output, limits and encoder settings follow compute.ndarray.sum.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2, 3], [3, 1, 1, 0, 4, 2]);
const indices = nd.argmin(a, { axis: 1 });
console.log(indices.data()); // [1, 0]
indices.destroy();
a.destroy();

See Also