how to make nan turn into 0 in array

5 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
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)

Categories

Find more on Measurements and Feature Extraction in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by