MATLAB Answers

Find Nonzero element of a signal in simulink - without using "Find" block

27 views (last 30 days)
Jiannan Zhang
Jiannan Zhang on 29 Aug 2016
Edited: Peter Anderson on 26 Apr 2018
Hi,
I need to find nonzero elements in Simulink of a Boolean vector signal, for example:
[1;0;1;0;0]
Basically it comes from comparison of two signals. This can be done by a find block in Simulink but I am only using blocks in the "slcilib" (simulink code inspector library) for code certifiable reasons, and of course it does not contain a find block.
What I expect to have is the index vector itself, in this case [1 3], and I will feed this index vector to a selector block index port (this port does not support Boolean data type!), to select corresponding values of another signal and assign these values.
Is it possible to do it using only math and logic operations that are contained in the slcilib? Any clue would be helpful!
Thanks!

  4 Comments

Show 1 older comment
Jiannan Zhang
Jiannan Zhang on 29 Aug 2016
Hi,
This is a real funny comment here. Firstly I am not doing this by the request of a "teacher". I need to use blocks in the slcilib since I want the code generated from the blocks to be certifiable. Secondly, I ask the question because I cannot solve the problem, and I suppose someone in the community may provide some clues about it.
If this is the way Mathworks provide services to its customers I would say this is a really bad customer experience, and I will try to let people know about it.
Cheers to the "Best comment ever from Simulink Community"
Azzi Abdelmalek
Azzi Abdelmalek on 29 Aug 2016
@Jiannan, the community does not represent Mathworks, we almost have the same status, except some earned privileges to edit or delete questions! In this forum, we try to avoid answering homework, but sometimes it's not clear if it's an assignment or not. So, there is nothing funny about Jhon's comment
Jiannan Zhang
Jiannan Zhang on 29 Aug 2016
@Azzi Thanks for the information. I am really new here in the community and I would apologize for whatever I am not doing right. I only think that there is no need to teach someone using words like a teacher himself, without knowing what is really the case. I suppose this is basic respect and behavior someone is to possess. If I am doing something wrong pls simply guide me to the rules and I will try to understand them, and this is my suggestion here to those who believe themselves to be capable of teaching strangers like their own kid, and I guess people with such behaviors do not deserve the so called privileges. Thanks.

Sign in to comment.

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 29 Aug 2016
Edited: Azzi Abdelmalek on 29 Aug 2016
You can use compare to zero block

  3 Comments

Jiannan Zhang
Jiannan Zhang on 29 Aug 2016
Hi,
Compare to zero gives me the Boolean type, which is already what I got after the comparison. And also I cannot simply send the signal to workspace or even use matlab functions to process it due to code tracibility issues (although matlab functions are included in the slcilib).
What I really need is the actual index as [1 3] for [1 0 1 0 0] for example, and this has to be done online. And then I will need to pass the index to a selector block to select the values of index [1 3] and then assign them.
Problem is that the selector index port does not support Boolean data type. I am actually thinking about doing it with if action block, but there are always unwanted 0 when I try to mux the signal, and the selector index port will give an error.
Azzi Abdelmalek
Azzi Abdelmalek on 29 Aug 2016
Edit
If yoursignal=[1 0 1 0 1 ], If the sample time is ts=0.1 for example to get the indices,
idx=10*ts*yoursignal=1*1=1
idx=10*2*ts*yoursignal=2*0=0
idx=10*3*ts*yoursignal=3*1=3
idx=10*4*ts*yoursignal=4*1=0
idx=10*5*ts*yoursignal=5*1=5
You get the indices 1,3 and 5. You will ignore the 0 value
NB: n*Ts is given by the clock
Jiannan Zhang
Jiannan Zhang on 29 Aug 2016
Hi,
Thanks for the idea. Actually a simple gain from 1 to 5 for each signal in order would give me the indices, but problem is that I still do not know how to really get rid of the zeros, since in the simulation the signal keeps updating and the indices of zeros are also updating. I will need to send the signal [1 0 3 0 5] to a Selector Index port, but the selector index port will report me a problem.
Using a find block as below, it works good as below, since find will ignore the zeros automatically:
Without a find block but simple gains, it will deliver an error such as:
Error while initializing parameters. Caused by: Element 1 of input port Idx1 in 'selfBuildFind_test2/Selector' has a value of 0. This value is not within the range of permissible values (1 through 6) that allows selection of a valid input data element
I have tried several ways of doing it including if subsystem, which only adds extremely up to the overall complexity. I would really appreciate if someone has a possible solution.

Sign in to comment.


Peter Anderson
Peter Anderson on 26 Apr 2018
Edited: Peter Anderson on 26 Apr 2018
See my answer to this question here:
Short answer is use a for-iterator subsystem. This version finds the first non-zero element and then stops looking.
Note that there will be a performance hit doing it this way over MATLAB function block with a find function call when simulating in normal mode. Not noticable for small arrays but I tested it for large arrays with a one in the final index and it was much slower. Running in accelerator mode was fine though.

  0 Comments

Sign in to comment.

Sign in to answer this question.