在 Simulink 中查找二维数组中第一个非零元素的索引

Find the index of the first non-zero element in a 2-D array in Simulink

我想在 Simulink 中找到二维数组中第一个非零元素的索引,就像我在 MATLAB 中使用 find(u1, 1) 命令一样。

例如,在 MATLAB 中我会这样做:

u1 = [46.15 61.21; 22.5 45.3; -1 -1; -1 -1; -1 -1];

idx= find(u1<0, 1) % The answer would be: idx = 3

您可以像这样使用 FindMinMax

输入应该是你的逻辑向量。可以使用任何先前的计算来获得这一点。

对于您的示例,您可能希望使用 Relational Operator 块来测试 < 条件,并使用输出代替上述输入。

Find 块将 return 非零元素的所有索引。然后可以使用 MinMax 块(设置为取最小值)来获取最小索引。


您可能想使用 Logical Operator 块,设置为 OR,以在使用 Find 之前测试任何值是否为真。如果没有值是真的,Find 将 return 一个空数组,这将在进入 MinMax 块时出错。这可以使用 If/Else If 逻辑来避免,如控制流的 documentation 所示。