不使用遍历,如何将Tuple里的负数元素全都变为0

假设有一个元组 a := [566, 52, -15, 50, -18, 0],如何将里面的负数都置为0,其他元素不变呢?

常见的思路是对元组的元素进行遍历。如果发现某个元素小于0,就强制给它赋值为0 。

其实Halcon里面有一些算子组合,可以更简洁地实现这个目的。

 1 a := [566, 52, -15, 50, -18, 0]
 2 tuple_length (a, Length)
 3 *生成一个所有元素都为0,长度为Length的新元组
 4 tuple_gen_const (Length, 0, Newtuple)
 5 tuple_greater_equal_elem (a, Newtuple, Greatereq)
 6 tuple_mult (Greatereq, a, a_greater)
 7 
 8 *下面的程序可以将a中的所有的正数元素都强制置为0
 9 tuple_less_equal_elem (a, Newtuple, Lesseq)
10 tuple_mult (Lesseq, a, a_Lesseq)

其中:

a_greater  :=  [566, 52, 0, 50, 0, 0]

a_Lesseq  :=  [0, 0, -15, 0, -18, 0]

其中涉及到了两个重要算子,分别是:

tuple_greater_equal_elem :测试一个元组是否按元素大于或等于另一个元组。如果是,则 Greatereq 中相应的元素为1,否则为0。

tuple_less_equal_elem :测试一个元组是否按元素小于或等于另一个元组。如果是,则 Lesseq 中相应的元素为1,否则为0。

原文地址:https://www.cnblogs.com/xh6300/p/13652811.html