2017-10-17 169 views
0

我有一个数组A.快速替代找出使用ismember阵列的位置在MATLAB

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]; 

正负局部极大值和最低高度。

Ypos =[ 5  7  8  4] 
Yposloc = [6 15 18 23 ] 
Yneg = [  0   0 6.2000 1.0000 ] 
Ynegloc = [2  9 16 20 ] 

现在我结合在一个阵列

Y = [0 5.0000   0 7.0000 6.2000 8.0000 1.0000 4.0000] 
Yloc = [2  6  9 13 15 18 20 23] 

然后我应用过滤器的所有极大值和最低高度和除去一些极大值和最低高度和separted本地极大值和最低高度与去除的位置

Ylocknew = [0 5.0000   0 7.0000  8.0000 1.0000 4.0000] 
Yloc = [2  6  9 15 18 20 23] 

或者只是有什么方法可以找出原始数组中这些过滤值的位置。

局部极大值和最低高度分离后我得到了这些阵列

Yposnew = [ 5  7  8  4] 
Ynegnew = [ 0  0  1] 

问题。

我要问的是有没有原始数组A,而不是循环找到Yposnew和​​位置作为循环服用大量时间任何快速的方法。

(请考虑我在原来的数据我有被重复,所以我需要的那些元素。)

请看看代码。

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]; 
t= 1:numel(A); 
X=A; 
[Ypos,Yposloc] = findpeaks(X); 
[Yneg,Ynegloc] = findpeaks(-X); 
Yneg = -1*Yneg; 
Y = zeros(1, max([Yposloc, Ynegloc])); 
Yloc = zeros(size(Y)); 
Yloc(Yposloc) = Yposloc; 
Yloc(Ynegloc) = Ynegloc; 
Y(Yposloc) = Ypos; 
Y(Ynegloc) = Yneg; 

Y = Y(Yloc ~= 0) % this is the combined signal what I am expecting 
Yloc = Yloc(Yloc ~= 0) % this is the combined locations what i am expecting 

% I have applied limits on combined local maxima and minima array 

for i = 1:numel(Y)-1 
%  if (Y(i+1)<=-6 && Y(i+1)>=8) 
    if (Y(i+1)>= (Y(i)-1) && Y(i+1)<=(Y(i)+1)) 
%    if (Y(i+1)<=-1 || Y(i+1)>=1) 
    Y(i+1)= inf; 

    else 
    Y(i)= Y(i); 
    end 
%  i=i+1; 
    end 
Ylocknew = Y; 

% I have calculated Filtered locations for filtered local maximas array 
for b = 1:numel(Yloc) 
if (Ylocknew(1,b) == inf) 
    Yloc(1,b) = inf; 
else 
    Yloc(1,b) = Yloc(1,b); 


end 
    b=b+1; 

end 

Yloc(Yloc==inf)= [] 
Ylocknew(Ylocknew==inf)= [] 


% I have separated positive and negative local maximas from limited array 

[Yposnew] = Ypos(ismember(Ypos, Ylocknew)); 
[Ynegnew] = Yneg(ismember(Yneg, Ylocknew)); 

需要一个快速的替代或快速的方法来计算原始阵列滤波后的局部极大值和最低高度的位置。

% Filtered Locations for FIltered positive local maximas 

for c = 1:numel(Yposnew) 
    if (Yposnew(c) == X(Yposloc(c))) 
     Yposlocfiltered(c)= Yposloc(c); 
    elseif (Yposnew(c) ~= X(Yposloc(c))) 
     d=c; 
     while(Yposnew(c) ~= X(Yposloc(d))) 
       d= c+1;    
     end 
       Yposlocfiltered(c)= Yposloc(d); 
    end 
end 

    % Filtered Locations for Filtered negative local maximas 

    for r = 1:numel(Ynegnew) 
    if (Ynegnew(r) == X(Ynegloc(r))) 
     Yneglocfiltered(r)= Ynegloc(r); 
    elseif (Ynegnew(r) ~= X(Ynegloc(r))) 
     s=r; 
     while(Ynegnew(r) ~= X(Ynegloc(s))) 
       s= r+1;    
     end 
       Yneglocfiltered(r)= Ynegloc(s); 
    end 
end 
+1

请阅读如何创建[MCVE],并要求* *一个特定*** ***问题。 – beaker

+0

非常感谢烧杯其实这个代码是一个例子,我试图阐明我的想法,我想做什么。我已更新并试图用拥挤的话语传达我的信息。请看看它。 – Peter

+0

当你删除最小值/最大值时,为什么你还没有删除位置数组中的索引? – beaker

回答

1

如果你退一步,看看你从ismember中间结果得到的信息,你已经得到了正确的位置的指标:

变量值就在ismember前行:

Ypos =[5 7 8 4] 
Yposloc = [6 15 18 23] 
Yneg = [0 0 6.2000 1.0000] 
Ynegloc = [2 9 16 20] 
Ylocknew = [0 5.0000 0 7.0000 8.0000 1.0000 4.0000] 
Yloc = [2 6 9 15 18 20 23] 

现在,而不是使用ismember返回的逻辑索引,然后扔掉,我们可以保存和使用它们的两倍:

posidx = ismember(Ypos, Ylocknew); % get locations of Ylocknew in Ypos 
% posidx = [1 1 1 1]; keep all Ypos values 
Yposnew = Ypos(posidx);   % save the values... 
Yposlocnew = Yposloc(posidx); % and save their corresponding locations 

negidx = ismember(Yneg, Ylocknew); % repeat for Yneg 
% negidx = [1 1 0 1]; we've removed Yneg(3) 
Ynegnew = Yneg(negidx);    % saving values... 
Yneglocnew = Ynegloc(negidx);  % and locations 

结果:

Yposnew = [5 7 8 4] 
Yposlocnew = [6 15 18 23] 
Ynegnew = [0 0 1] 
Yneglocnew = [2 9 20]