2

Can someone help me to provide an efficient way or help me to perform the provide code to do make same results in minimal possible steps. I shall be grateful to you.

I have an Original Array:

A = [1 1 1 4.3 4.5 4 4.3 3 1 0 0 2 6.2 6.3 6 6.2 7.4 8 7.2 2 2 3 3 2];

Output Looks like:

A = [1 1 1 4 4 4 4 3 1 0 0 2 6 6 6 6 6 7 7 2 2 3 3 2];

I apply some restrictions and removed some values from array of local maxima’s after that I received some new arrays.

Yposlocfiltered = [6    15    23];
idx             = [4     6     3];
Yneglocfiltered = [2     9    20];
idx_neg         = [1     1     2];

Where I will find idx(local maxima value) I will check if values behind and ahead are greater make a window.

Example

If I will find 4 and 4.5, 4.3 is greater than 4 include it in backward window 
4.3 is greater than 4 include it in forward window.

I need to find range of values behind local maxima and ahead local maxima.

I have tried to write a code that’s works fine but issue is that it’s too long.

Can someone please provide me an idea to perform this action in minimal steps and in faster ways?

I have only provided code for positive local maxima’s as for negative local maxima code Is just replica of this.

Code:only for positive local maximas

 clc
 clear all

 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];
 idx = [4     6     3];
 Yneglocfiltered = [2     9    20];
 idx_neg = [1     1     2];

 for b = 1: numel(idx)
 for c = 1:numel(A)
 f = Yposlocfiltered(1,b)-c ;
 g = Yposlocfiltered(1,b)+c ;

 %   if (f > 0 && g <= numel(X))
 if(f > 0)   
 if  (A(Yposlocfiltered(1,b)-c))> idx(1,b)

 else 
   d= f+1;
   z(b)= d;
   backward_window = z;
  break
 end        

 end

 end 

 end 

 for r = 1: numel(idx)
 for s = 1:numel(A)
 u = Yposlocfiltered(1,r)-s ;
 v = Yposlocfiltered(1,r)+s ;

 %   if (f > 0 && g <= numel(X))
 if(v <=numel(A))   
 if  (A(Yposlocfiltered(1,r)+s))> idx(1,r)

 else 
   w= v-1;
   y(r)= w;
   forward_window = y;
 break
 end        

 end

 end 

end 

n=4
for i=1:length(backward_window)
range = backward_window(i): forward_window(i);
p = range 
if n <= numel(p)
        p = range(1:n)
         A( p) = idx(i);
else
%   if (size(range)<= 3)
A( p) = idx(i);
end 
end
Peter
  • 161
  • 1
  • 11

1 Answers1

1

From the first look at your code, I believe you can combine your first two for loops into one.

sA = numel(A);
sI = numel(idx);

for i = 1:sI
    f = Yposlocfiltered(i) - [1:sA];
    g = Yposlocfiltered(i) + [1:sA];
    f(f<1) = [];
    g(g>sA) = [];
    backward_window(i) = f(find(A(f) <= idx(i), 1)) + 1;
    forward_window(i) = g(find(A(g) <= idx(i), 1)) - 1;
end

Here, you can use find to locate the element of an array matching the specified condition, i.e. g <= numel(X) or A(f) <= idx(i).

Your last loop which modifies A can also be integrated into the same loop, so you can have:

sA = numel(A);
sI = numel(idx);

n=4;
for i = 1:sI
    f = Yposlocfiltered(i) - [1:sA];
    g = Yposlocfiltered(i) + [1:sA];
    f(f<1) = [];
    g(g>sA) = [];
    backward_window(i) = f(find(A(f) <= idx(i), 1)) + 1;
    forward_window(i) = g(find(A(g) <= idx(i), 1)) - 1;
    range = backward_window(i) : forward_window(i);
    if n <= numel(range)
        A(range(1:n)) = idx(i);
    else
        A(range) = idx(i);
    end
end
Anthony
  • 3,595
  • 2
  • 29
  • 38
  • 2
    Anthony first of all sorry for a bit late reply as i was away from my PC . Thanks a lot for your such a great and accurate and mature reply. It worked perfect for me. Thanks for sparing your time and efforts for me. – Peter Oct 19 '17 at 20:21