Assigning Values to the neighbors same value in MATLAB

断了今生、忘了曾经 提交于 2019-12-13 20:17:08

问题


I am having a small issue but I am clueless where I am at fault. Can someone please guide me the right way? Thanks in advance.

What I have done.

My codes finds local maxima’s.

Bring down from local maxima to a certain point.

Assign the neighbors that are greater than the downsized value, the value of downsized point.

Small Example

X = [1 0 1 4.3 4.5 5 4.3 4.2 0 0 0 2 6.2 6.3 7 6.2 7.4 8 7.2 1 2 3 4 2];

Local maxima’s are 5, 7, 8, and 4

Go down to certain point. Like 4, 6, 7, 3.

Assign neighbors that have values greater than 4,6,7,3 same values.

Output will be like

X = [1 0 1 4 4 4 4 4 0 0 0 2 6 6 6 6 6 6 6 1 2 3 3 2];

As this was a simple example so did not have much difference when I applied my code on it.

I have provided another example with my code. Please have a look at that.

 t = 50;
 X = sin(2*pi*10*(1:t)/t) + 0.5*sin(2*pi*5*(1:50)/t) - linspace(0,3,50);

 % plot(X)
 A= X;
 %   A1 = [1 0 1 4.3 4.5 5 4.3 4.2 0 0 6 6 6 6 6 6 6 6  6 1 2 3 4 2];
 [pks,locs] = findpeaks(A);
 idx = A(locs)-0.3  ; % will bring local maxima to the point where we want 

 for b = 1: numel(idx)  % loop for checking idx 
 for c = 1:numel(A)  % loop for checking neighbourhoods

  if (A(locs(1,b)-c)> idx(1,b) && A(locs(1,b)+c)> idx(1,b))

  else  
      d= c-1;
      z(b)= d;
     break
  end


  end
  end
  for i = 1:numel(idx)

  for n = 1:z(1,i)
       idx_1 = A(locs(i) - n); % will find what is in neighbourhood of local   maxima
        idx_2 = A(locs(i) + n);
 %         idx_1 = A(locs - n); % will find what is in neighbourhood of local maxima
 %         idx_2 = A(locs + n);
    if (idx_1>idx(1,i) && (idx_2>idx(1,i)))  % Check and assign the same value to neighbourhood of local maxima
       A(locs(1,i)) = idx(i);
       A(locs(1,i)-n) = idx(i);
       A(locs(1,i)+n) = idx(i);
%            A(i)) = A(idx(i))
   else if (idx_1 < idx(1,i) && (idx_2>idx(1,i)))
        A(locs(1,i)) = idx(i);
        A(locs(1,i)+n) = idx(i);   
       else if (idx_1 > idx(1,i) && (idx_2<idx(1,i)))
        A(locs(1,i)) = idx(i);
        A(locs(1,i)-n) = idx(i);        
       else
        A(locs(1,i)) = idx(i); 
        A(locs(1,i)-n) = idx_1(i);
        A(locs(1,i)+n) = idx_1(i);
       end
      end

  end
  end
         A(locs(1,i)) = idx(i);
  end

  figure(1)
  hold on 
  plot(X,'r')
  plot(A,'b')
  hold off

Expected Output be like Black line in the picture not blue lines that are formed from code

来源:https://stackoverflow.com/questions/46642098/assigning-values-to-the-neighbors-same-value-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!