how to change the number that decreases in the array element to the same number, for example 0 (zero)

hello everyone,
I have an array element value as in the following code and this number will keep decreasing to about 10e-6, there should be a lot up to (1x500) but I just gave a few examples with this,
is there an easy way to change it to the same number eg 0 (zero)?
So in general it is not if it is worth below 1, for example, then it is zero, not like that, but if it is worth decreasing
Thank you very much
a = [100.001469926008 0.0140073495254864 0.00452326089811489 0.00228582409151486 0.00157249586126199 0.00121055360392781 0.000988328854777707 0.000836846113296335 0.000726496714817108]
if %there are array elements that are keep decreasing
%then everything changes to 0 so it remains (1x500) but everything is 0
end

Answers (1)

Is something like this what you are asking for?
%%
a = [100.001469926008 0.0140073495254864 0.00452326089811489 ...
, 0.00228582409151486 0.00157249586126199 0.00121055360392781 ...
, 0.000988328854777707 0.000836846113296335 0.000726496714817108 ];
%%
daff = diff(a);
ix = find( a<1, 1, 'first' ); % "worth below 1"
if all( daff(ix:end) < 0 ) % "keep decreasing"
a(ix:end)=0; % "changes to 0"
end
it returns
>> a
a =
Columns 1 through 5
100 0 0 0 0
Columns 6 through 9
0 0 0 0
Based on the comments below I think the best answer is:
%%
daff = diff(a);
if all( daff < 0 ) % "keep decreasing" monotonically decreasing
a(:) = 0; % "changes to 0" "(:)" is needed to replace ALL values by 0
end

14 Comments

I am very grateful for your generosity @per isakson, but I want to ask a little more if then I want to make everything 0 even for 100, do I only need to delete the ones worth below 1 or what? Thank you very much. or maybe the example is this
%%
a = [100.001469926008 0.0140073495254864 0.00452326089811489 ...
, 0.00228582409151486 0.00157249586126199 0.00121055360392781 ...
, 0.000988328854777707 0.000836846113296335 0.000726496714817108 ];
%%
daff = diff(a);
if all( daff < 0 ) % "keep decreasing"
a = 0; % "changes to 0"
end
I'm trying like this so that it goes zero and doesn't get into findpeaks but why is it still an error? @per isakson
function [dist,peak2,locs2] = fcn(u)
daff = diff(u);
ix = find( u<1, 1, 'first' ); % "worth below 1"
if all( daff(ix:end) < 0 ) % "keep decreasing"
u(ix:end)=0; % "changes to 0"
end
locs = zeros(size(u));
pks = zeros(size(u));
if u ~= 0
[pks,locs] = findpeaks(u);
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;
and the error is
Your example won't run, because ix isn't defined. (This comment is in response to your first version of your first comment.)
%%
daff = diff(a);
ix = find( a<1, 1, 'first' ); % "worth below 1"
if all( daff(ix:end) < 0 ) % "keep decreasing"
a(1:end)=0; % "changes to 0" from the first element
end % ^
or rather
%%
daff = diff(a);
if all( daff < 0 ) % "keep decreasing" decreases from first to last element
a(:) = 0; % "changes to 0" "(:)" is needed to replace ALL values by 0
end
sorry when I tried to use yours from the second one I found that the array value was (1-1) with an error like this
then I tried to add it to be the same as the one above but he said there was still one missing what then needed to be added again please help me a little more
function [dist,peak2,locs2] = fcn(u)
locs = zeros(size(u));
pks = zeros(size(u));
daff = diff(u);
if all( daff(size(u)) < 0 ) % "keep decreasing" decreases from first to last element
u(:) = 0; % "changes to 0" replace all values by 0
end
if u ~= 0
[pks,locs] = findpeaks(u);
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;
the error is like this
so that I think there is still one more, I beg you very much for your help. If it works, I will wish you all the goodness @per isakson
I also don't understand why the array element is 1-1 even though the time in the example above is coded for 1-9 @per isakson
is it possible that there is less one value that does not decrease in value @per isakson
In response to your second comment. (What has Simulink to do with this. I use an old time m-file.)
>> [dist,peak2,locs2] = fcn(a)
dist =
0
peak2 =
0
locs2 =
0
where
function [dist,peak2,locs2] = fcn(u)
daff = diff(u);
if all( daff < 0 ) % "keep decreasing"
u(:) = 0; % "changes to 0"
end
locs = zeros(size(u));
pks = zeros(size(u));
if not( all( u == 0 ) ) % you tested only on the first element of u
[pks,locs] = findpeaks(u);
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;
end
My code at the beginning is a good code that can detect peaks and locs well but when the input condition continues to decline so there is no peak so findpeaks is useless so I want to create a condition where everything whose value decreases becomes 0 but keeps the array condition is (1x501) so locs (2) etc. can have a value of 0 @per isakson
"error which is an array of (1-1)," What array? See TUTORIAL: how to ask a question (on Answers) and get a fast answer
I cannot run your Simulink model on R2018b.
daff is one element shorter than u (see help on diff). I never proposed size(u) in all(daff(size(u)).
I have changed my simulink to r2018b I hope you want to see it first so you understand what I mean @per isakson
I will also pay attention to the tutorial you submit
I see there are indeed some bad things I do like sending my simulink, but I just mean for you to check if then if something goes wrong with my work because some of the suggestions you have given are actually very good but I don't know why it's still not worked too @per isakson
therefore I apologize for breaking a few things there because I have been asking this for a long time about 2 weeks ago
because maybe what is in Matlab is different from what is in Simulink
"I have been asking this for a long time about 2 weeks ago"
Several people have put a fair amount of effort into helping you. They haven't contributed to answer this question. I guess, you lost them.
"because maybe what is in Matlab is different from what is in Simulink"
Communication is difficult. What does this paragraph say?
"have changed my simulink to r2018b"
I've run your model and browsed the diagnostics. It's above my head to debug this model.
See my addendum to the answer.
okay, thank you very much for all the help. I'm sorry for always asking and troubling you @per isakson

Sign in to comment.

Products

Release

R2019b

Asked:

on 19 Jan 2021

Commented:

on 20 Jan 2021

Community Treasure Hunt

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

Start Hunting!