Sorry for asking question again but it is different from my previously asked question just the arrays are same but query is different.
I have an original array
A = [1 0 1 4.3 4.5 5 4.3 3 0 0 0 2 6.2 6.3 7 6.2 7.4 8 7.2 1 2 3 4 2];
Expected Output from code:
A = [1 1 1 4 4 4 1 1 1 0 0 2 6 6 6 6.2 7.4 8 2 2 2 3 3 2];
I have found Positive Local maxima’s and negative Local maxima’s in an array A
and after that I applied some limitations and removed some maxima’s and minima’s and the formed an array with local maximas and minimas in order .
Array Containing Positive and negative local maxima’s in order along with location.
Yloc = [2 6 9 15 20 23];
Ymaximaminimanew = [0 5 0 7 1 4];
Now I separated locations of positive and negative local maximas from Yloc
.
Yposlocfiltered = [ 6 15 23];
Yneglocfiltered = [2 9 20];
What I want:
I want to make a loop that checks the values in Ymaximaminimanew if negative local maximas comes first then do certain action that is linked with negative local maxima and if the value belongs to positive local maxima then do certain action that belongs to positive local maxima.
Example
Like in Ymaximaminimanew first is 0 which belongs to negative local maxima
then do the action. Please have a look at attached code it will go to elseif
and perform the task.
2nd value is 5 which is positive local maxima so it will go to if and
perform the task.
The logic I have used is provided in the code. If someone suggest me better logic to make my code smarter then it will be nice.
I have some issue with the code I am not getting the desired output what I am expecting. Just a small error may be in formatting of loop.
I have some issue with loop 4 when t=4 its starts misbehaving also If someone let me know how to do this smartly in simple and minimal steps then it will be really nice.
Thanks a lot in advance for your time, expertise and help.
Code:
A = [1 0 1 4.3 4.5 5 4.3 3 0 0 0 2 6.2 6.3 7 6.2 7.4 8 7.2 1 2 3 4 2];
Yposlocfiltered = [ 6 15 23];
Yneglocfiltered = [2 9 20];
Yloc = [2 6 9 15 20 23];
Ymaximaminimanew = [0 5 0 7 1 4];
sA = numel(A);
n=3;
r = 1;
s = 1;
for t = 1:numel(Yloc)
if (ismember(A(Yloc(1,t)),A(Yposlocfiltered)))
x = A(Yposlocfiltered);
x = x(1,r);
poslocations=x ;
idx = poslocations-1;
sI = numel(idx);
f = Yposlocfiltered(1,r) - [1:sA];
g = Yposlocfiltered(1,r) + [1:sA];
f(f<1) = [];
g(g>sA) = [];
backward_window(t) = f(find(A(f) <= idx, 1)) + 1;
forward_window(t) = g(find(A(g) <= idx, 1)) - 1;
r = r+1;
range = backward_window(t) : forward_window(t);
if n <= numel(range)
A(range(1:n)) = idx;
else
A(range) = idx;
end
elseif (ismember(A(Yloc(1,t)),A(Yneglocfiltered)))
c = A( Yneglocfiltered);
c = c(1,s);
neglocations=c;
idx_neg = neglocations+1;
sU = numel(idx_neg );
h = Yneglocfiltered(1,s) - [1:sA];
p = Yneglocfiltered(1,s) + [1:sA];
h(h<1) = [];
p(p>sA) = [];
backward_window_neg(t) = h(find(A(h) <= idx_neg, 1)) + 1;
forward_window_neg(t) = p(find(A(p) <= idx_neg, 1)) - 1;
range = backward_window_neg(t) : forward_window_neg(t);
s = s + 1;
if n <= numel(range)
A(range(1:n)) = idx_neg;
else
A(range) = idx_neg;
end
end
end