0

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 
Peter
  • 161
  • 1
  • 11
  • 2
    How is this different from your [previous question](https://stackoverflow.com/questions/46831439/dynamic-window-forming-in-efficient-way-in-matlab)? – sco1 Oct 19 '17 at 23:40
  • 1
    Please provide the *minimal* code required to demonstrate your problem and please properly indent and comment your code. You're making it much more difficult for us to understand your problem. – beaker Oct 20 '17 at 15:32
  • Sorry for late response . Beaker Thanks a lot . Next time I will try to ask question in simplest possible way. Luckily i remained able to solve this issue . If you allow may i remove this post as may be i remained unable to explain my qurrey properly. Thanks a lot you always remained really helpful and sorry from myside. – Peter Oct 20 '17 at 19:13
  • excaza Sorry for a bit late response. This qurrey was a bit different from previously asked question but unfortunately i remained able to explain it properly. I remained able to solve this issue . May i request from you to let me remove this question . Next time i will try to be to the point and as mature as possible . Thanks in advance. – Peter Oct 20 '17 at 19:15
  • @Peter You can always delete your question as long as there is no upvoted answer. If your question has been downvoted, you might want to try to improve the question and get the score up, but in this case there's been no up or down votes. – beaker Oct 20 '17 at 21:14

0 Answers0