在MATLAB中高效地进行动态窗口形成 [英] Dynamic window forming in efficient way in MATLAB

查看:131
本文介绍了在MATLAB中高效地进行动态窗口形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我提供一种有效的方法还是可以帮助我执行提供的代码,从而以尽可能少的步骤获得相同的结果.我将感谢您.

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.

我有一个原始数组:

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

输出看起来像:

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

我将在哪里找到idx(局部最大值),我将检查后面和后面的值是否更大以创建一个窗口.

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

示例

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.

我试图编写一个可以正常工作的代码,但问题是它太长了.

有人可以给我一个思路,以最少的步骤和更快的方式执行此操作吗?

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.

代码:仅适用于正局部最大值

 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

推荐答案

从第一次查看代码开始,我相信您可以将前两个for循环合并为一个.

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

在这里,您可以使用 find 来找到匹配指定条件(即g <= numel(X)A(f) <= idx(i))的数组的元素.

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).

您修改A的最后一个循环也可以集成到同一循环中,因此您可以:

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

这篇关于在MATLAB中高效地进行动态窗口形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆