# How to select elements from a vector repecting a condition in Simulink

2 views (last 30 days)
Josselin on 9 Apr 2014
Commented: Niklas Nylén on 11 Apr 2014
Hi,
here is my problem:
I have in input a vector of size 31, filled alternatively with zeros or doubles (e.g. [2 8 3 0 0 0 0 0 5 7...]). What i need to do is sum the different series of non-zeros values and output them in another vector, keeping zeros between those values. With the previous vector, that would give: [13 0 12...].
I also know that there will be a problem if the number of series isn't always the same, but i think i could find a trick to avoid that problem. I did that in a Matlab function embedded in my Simulink code, but I heared Simulink code was much more performant than Matlab for embedded systems.
Do you have any idea on how I could do this?
Josselin

Niklas Nylén on 9 Apr 2014
Edited: Niklas Nylén on 9 Apr 2014
Although it is possible to do what you want without using simulink blocks it is a bit tricky. You could start by attempting to speeding up the simulation by using the information found in this answer: http://www.mathworks.com/matlabcentral/answers/97224
Josselin on 9 Apr 2014
Hi,
Do you know happen to know if a Matlab embedded function could cause the malfunctionnment of a program in an embedded system using simulink code?
Just to be curious, how would you proceed, roughly, to do it the tricky way using only simulink?
Niklas Nylén on 11 Apr 2014
It is probably difficult to avoid some kind of loop. I would start by just attempting to get variable length vectors to be assigned in a simulink for loop. After that it should just be a matter of which values that should be assigned to the output vector (i.e. assign to the vector if the current value is not equal to zero or if it is equal to zero and the previous value is not equal to zero.

### Categories

Find more on Sources in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!