0
distances = sqrt((x - max(x)).^2 + (y - max(y)).^2);
    [peaks, iPeaks] = findpeaks(distances);%to find out where the curve turns around
          for i = 1 : length(iPeaks)-1
                    iPeaks1 = iPeaks(i);
                    iPeaks2 = iPeaks(i+1)-1;%analyse of consecutive pair of peaks
                     %skip small noise peaks
                   if length(iPeaks1:iPeaks2)>=5 
                      xx=x(iPeaks1:iPeaks2)
                       yy=y(iPeaks1:iPeaks2)
                   end
                end

你好

我需要构造向量 xx 和 yy。问题是在每个周期中,过去的 xx 和 yy 都会被删除,但我想要相反。我希望他们保留过去的信息并在每个周期中成长。我能做些什么?而且我知道我应该预先分配 xx 和 yy。

我感谢任何帮助。非常感谢您。

* *这是一个可能的解决方案。这里的问题是我需要预先分配。但如果我这样做,xx 和 yy 会保持零并继续增长“内部有零”,这是错误的

distances = sqrt((x - max(x)).^2 + (y - max(y)).^2);
[peaks, iPeaks] = findpeaks(distances);%to find out where the curve turns around
xx=[];
yy=[];
for i = 1 : length(iPeaks)-1
        iPeaks1 = iPeaks(i);
        iPeaks2 = iPeaks(i+1)-1;%analyse of consecutive pair of peaks
         %skip small noise peaks
       if length(iPeaks1:iPeaks2)>=5 
          xx = [xx; x(iPeaks1:iPeaks2)];%''concatenate''(connect)
          yy = [yy; y(iPeaks1:iPeaks2)];
       end
end
4

3 回答 3

1

使用串联很容易实现向量的增长:

x = [1, 2, 3];
y = [4, 5, 6, 7];
z = [x, y]; %# z will be [1, 2, 3, 4, 5, 6, 7]

在你的情况下,你会做

%# Start with an empty vector.
xx = [];
for i = 1 : length(iPeaksNoise)-1
    [...]
    xx = [xx, x(iPeaks1:iPeaks2)];
end

要独立于iPeaksNoise(即无论它是行向量还是列向量)的形状,您可以使用

for [...]
    t = x(iPeaks1:iPeaks2);
    xx = [xx; t(:)];
end

预分配意味着创建一个零向量,它具有最终输出的长度。你会做这样的事情

final_size = what_so_ever;
xx = zeros(final_size, 1);
start_index = 1;
for [...]
    t = x(iPeaks1:iPeaks2);
    xx(start_index : start_index + length(t) - 1) = t;
    start_index = start_index + length(t);
end

但是,在您的情况下,您还有一个额外的困难,即事先不知道最终大小xx,因为您不知道length(iPeaks1:iPeaks2)>=5循环内完成的频率。

编辑:

预分配的代码xx可能如下所示:

distances = sqrt((x - max(x)).^2 + (y - max(y)).^2);
[peaks, iPeaks] = findpeaks(distances);

%# Compute the distance between two peaks.
peakDistance = diff(iPeaks);
%# Filter peaks which are of length 5 or less.
peakMask = peakDistance > 5;
finalSize = sum(peakDistance(peakMask));
%# Copy the values in a new vector.
xx = zeros(1, finalSize);
idx = 1;
for count = 1 : length(peakDistance)
  if peakMask(count)
    pD = peakDistance(count);
    xx(idx : idx + pD - 1) = x(iPeaks(count) : iPeaks(count) + pD - 1);
    idx = idx + pD;
  end
end
于 2012-10-04T11:27:38.327 回答
0

您需要明确说明要放置数据的位置xxyy从您的代码中很难理解您想要它的位置,但它应该看起来像这些行

start = some_index_in_xx;
v = x(iPeaks1:iPeaks2);
xx(start:start+numel(v)-1)=v;

对于yy.start你必须自己计算。

另一方面,如果您想将新的xy矢量部分附加到 and 的末尾xxyy您可以这样做

xx = [xx x(iPeaks1:iPeaks2)];
yy = [yy y(iPeaks1:iPeaks2)];

您必须在循环之前xx进行初始化:yy

xx = [];
yy = [];
于 2012-10-04T11:19:46.000 回答
0

在这里,我假设您要在每次迭代中添加到 xx 和 yy 的元素数量每次都不相同。然后,如果你想预先分配空间,你需要找出你要加起来的空间。所以代码会因为额外的循环而变得更加复杂。一个版本可能是

x=(5*sin(0:(pi/100):2*pi)+rand(201,1)'-0.5)';
y=(5*cos(0:(pi/100):2*pi)+rand(201,1)'-0.5)';
%%
distances = sqrt((x - max(x)).^2 + (y - max(y)).^2);
[peaks, iPeaks] = findpeaks(distances);%to find out where the curve turns around

counterx = 0;
for i = 1 : length(iPeaks)-1
    iPeaks1 = iPeaks(i);
    iPeaks2 = iPeaks(i+1)-1;%analyse of consecutive pair of peaks
    %skip small noise peaks
    if length(iPeaks1:iPeaks2)>=5
        counterx =counterx+ iPeaks2-iPeaks1+1;
    end
end
xx=zeros(counterx,1);
yy=xx;
counterx=0;
clc
for i = 1 : length(iPeaks)-1
    iPeaks1 = iPeaks(i);
    iPeaks2 = iPeaks(i+1)-1;%analyse of consecutive pair of peaks
    %skip small noise peaks
    if length(iPeaks1:iPeaks2)>=5
        %         xx = [xx; x(iPeaks1:iPeaks2)];%''concatenate''(connect)
        %         yy = [yy; y(iPeaks1:iPeaks2)];
        counterx=counterx+1;
        xx(counterx:counterx+iPeaks2-iPeaks1) = x(iPeaks1:iPeaks2);
        yy(counterx:counterx+iPeaks2-iPeaks1) = y(iPeaks1:iPeaks2);
        counterx=counterx+iPeaks2-iPeaks1;
    end
end
disp([xx yy]')

但是,这是matlab。你不应该做所有这些。这是一些使用逻辑标志执行相同操作的代码,并且只需一次删除所有小峰值:

% find all the iPeaks that are 5 apart from the next one
peakdiffs = find((iPeaks(2:end)-iPeaks(1:end-1)-1)>=5);
% make a logical array quickly with same size as x
removeflag = x<=inf;
for i=1:numel(peakdiffs)
    % for each peak that is bigger than your threshold, those parts of the
    % logical array will be made false.
    removeflag(iPeaks(peakdiffs(i)):iPeaks(peakdiffs(i)+1)-1)=false;
end
%remove all indices in x,y that are still false in removeflag and store in xx
%and yy resp
xx= x; xx(removeflag)=[];
yy= x; yy(removeflag)=[];
disp([xx yy]')
于 2012-10-04T17:31:48.057 回答