Index for array max (or min) in Simulink without variable-size signal or MATLAB Function block

146 ビュー (過去 30 日間)
In Simulink I have a signal that is a 1-D array. I need to get the index associated with the first maximum (or minimum) value in the array. In MATLAB, that's simply this:
>> [val,idx] = max([2 3 6 8 4 7 8 4 5 6])
val =
8
idx =
4
where idx is the item I'm looking for.
Trying to do that in Simulink seems to be much less straightforward. The MinMax block does not offer an option to output the index of the first maximum value.
Coupling a MinMax with a Find Nonzero Elements blocks as in the suggestion on this page does do the trick, but it comes with the problem that the output of the find block is a variable-sized signal. I don't want it to be a variable sized signal; I just want the first index.
One could also use a MATLAB Function block and use the MATLAB min or max function as above, but MATLAB Function blocks take obnoxiously long to compile and often generate code with a lot of gunky overhead.
Is there not a simple, clean, cheap way to get the first max index?
  1 件のコメント
Michael
Michael 2017 年 10 月 3 日
FYI to anyone who stumbles across this page...MathWorks staff saw this question and generated an enhancement request for the MinMax block on 2 OCT 2017. Even if that eventually gets added to base Simulink, it doesn't help me in the near term at all.

サインインしてコメントする。

採用された回答

Peter Anderson
Peter Anderson 2018 年 4 月 26 日
I was looking at this sort of problem (for me it was find the first non-zero element) and came up with a solution that doesn't involve toolboxes, MATLAB function blocks or variable size signals. You can solve it with a for-iterator subsystem.
Your min/max case will be slightly different but will have very similar structure.
  3 件のコメント
Juan Ocampo
Juan Ocampo 2018 年 12 月 4 日
Congratulations great solution, thank you very muck for shearing it, i was thinking for a solution to this problem for a while and your solution is perfect.
Mike Shives
Mike Shives 2019 年 7 月 10 日
Thank you! This saved me hours!

サインインしてコメントする。

その他の回答 (4 件)

sl
sl 2019 年 2 月 13 日
Hi Michael,
The logic below will fulfill your need.
Capture.PNG
  2 件のコメント
Robin Müller
Robin Müller 2020 年 7 月 29 日
Thx, way better solution
Dick Benson
Dick Benson 2020 年 8 月 5 日
Good solution.
The Index Out really needs to be part of the min/max blocks.
One could use the sort block, but what a waste of CPU cycles.

サインインしてコメントする。


Marco
Marco 2017 年 10 月 3 日
Hello Michael, you can try to use Maximum and Minimum block (DSP System Toolbox).
BR Marco
  1 件のコメント
Michael
Michael 2017 年 10 月 3 日
Those blocks do look like they'll do the job. Unfortunately I don't have the DSP System Toolbox so it's not really a viable solution for me. It seems incredible that you can't get the capability I'm looking for without an inefficient workaround like a MATALB Function block or buying an extra toolbox, but that seems to be the case...

サインインしてコメントする。


Kirankumar Bacchewar
Kirankumar Bacchewar 2021 年 10 月 15 日
編集済み: Kirankumar Bacchewar 2021 年 10 月 15 日
This model can be used to find min,max and its Index. Just modify the size of the input array in selector blocks. Hope its helpful.

Mohammad Ali Sadeghian
Mohammad Ali Sadeghian 2023 年 4 月 27 日
Hello,
you can use 2-D Maximum block with value and index.
ALI

カテゴリ

Help Center および File ExchangeSignal Attributes and Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by