how to make nan turn into 0 in array

2 views (last 30 days)
Naufal Arfani
Naufal Arfani on 17 Jan 2021
Edited: dpb on 22 Jan 2021
Hello all,
im working in simulink with findpeaks and I have the code as below, for the condition if u ~ = 0 it works well, so the else condition is in the condition if u == 0,
but then there is a nan value outside u ~ = 0 so you need to add elseif,
but when I tried with isnan (u) it couldn't detect that he was nan and I wanted to change it to all 0 values
so that there is an error as shown below,
can anyone help me find out where it went wrong thank you very much
function [dist,peak2,locs2] = fcn(u)
locs = zeros(size(u));
pks = zeros(size(u));
if u ~= 0
[pks,locs] = findpeaks(u);
elseif isnan(u)
locs(size(u))= 0;
pks(size(u)) = 0;
else
locs(size(u))= 0;
pks(size(u)) = 0;
end
a = locs(2)- locs(1);
b = pks(1);
c = locs(1);
dist = a;
peak2 = b;
locs2 = c;
  35 Comments
dpb
dpb on 21 Jan 2021
Edited: dpb on 22 Jan 2021
Don't "just try" something; first sit down and design what it is that your application needs and expects on input and what the outputs are going to be, then write the code to do that.(*)
Remember that SIMULINK expects the same number of outputs all the time, every time unless you build a model that can switch between blocks depending upon what is returned.
As it is now, there's no place to return anything about more than two peaks as one set of location/magnitude values and a distance between that peak and another. If you add more, you have to do something about that...what is your call.
That's the thing -- what the function returns is totally arbitrary and up to you to decide what that should be and how many results are to be returned -- BUT: it has to have the same number every time and the model using those downstream has to know what they mean in order for it to make any sense. And only you know what any of it is supposed to mean and why there are a variable number of peaks and whether you're returning the correct ones each time the routine is called...as above, it's one thing to have something that runs; ensuring that it returns the correct answer is an entirely different issue.
The thing about the NaN is a complete red-herring -- the problem is/was totally one of the function not following the SIMULINK rules regarding the number of returned variables/signals being constant--so SIMULINK made up something for the missing ones.
(*) And, when you have done that, before running the simulation, test the MATLAB function thoroughly externally to ensure it behaves as you want it to with a sample of all of the types of input spectra the model will pass into it. Then, and only then, will you be able to know why you get what you get back instead of just throwing stuff blindly into a more-or-less black box and seeing what happens. Introduce some discipline and order into the process.
Athrey Ranjith Krishnanunni
@Naufal Arfani I changed the alfa step input to have an initial value of 1 and final value of 5, and u does indeed have several peaks, but like dpb said, what to do with that information is not something I'd be able to tell you, because I don't know the physics of the system that you are simulating. That is not a programming problem and you'll probably have to figure that out on your own.
As to the other model that you attached for the NaN issue, it's in R2019b again. 😌

Sign in to comment.

Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!