File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -255,6 +255,28 @@ proc contains*[T](t: Tensor[T], item: T): bool {.inline.}=
255
255
return find(t, item) >= 0
256
256
257
257
proc ismember* [T](t1, t2: Tensor[T]): Tensor[bool ] {.noinit.} =
258
+ ## Element-wise check whether elements of a Tensor are contained in another Tensor
259
+ ##
260
+ ## Inputs:
261
+ ## - t1: Tensor whose elements will be looked for, one by one, in `t2`
262
+ ## - t2: Tensor in which elements of `t1` will be looked for
263
+ ##
264
+ ## Result:
265
+ ## - A boolean tensor of the same shape as `t1`.
266
+ ## Each element indicates if `t2` contains the `t1` element that
267
+ ## is found in that particular position.
268
+ ##
269
+ ## Example:
270
+ ## ```nim
271
+ ## let t1 = arange(6).reshape(2, 3)
272
+ ## let t2 = [-3, 0, 2, 5].toTensor
273
+ # #
274
+ # # echo t1.ismember(t2)
275
+ # # # Tensor[system.bool] of shape "[2, 3]" on backend "Cpu"
276
+ # # # |true false true|
277
+ # # # |false false true|
278
+ # # ```
258
279
result = newTensor[bool ](t1.len)
259
280
for n, it in t1.enumerate():
260
281
result [n] = it in t2
282
+ result = result .reshape(t1.shape)
You can’t perform that action at this time.
0 commit comments