Index for array max (or min) in Simulink without variable-size signal or MATLAB Function block
이전 댓글 표시
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?
채택된 답변
추가 답변 (4개)
sl
2019년 2월 13일
5 개 추천
Hi Michael,
The logic below will fulfill your need.

댓글 수: 2
Robin Müller
2020년 7월 29일
Thx, way better solution
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
2017년 10월 3일
1 개 추천
Kirankumar Bacchewar
2021년 10월 15일
편집: Kirankumar Bacchewar
2021년 10월 15일
0 개 추천
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
2023년 4월 27일
0 개 추천
Hello,
you can use 2-D Maximum block with value and index.
ALI
카테고리
도움말 센터 및 File Exchange에서 Signal Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
