如果循环matlab连接向量

时间:2012-10-04 11:11:30

标签: matlab vector concatenation

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

3 个答案:

答案 0 :(得分: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的形状(即无论是行还是行 column-vector)你可以使用

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

答案 1 :(得分:0)

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

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

yy也一样。 start你必须自己计算。

另一方面,如果您要将新的xy向量部分附加到xxyy的末尾,则可以执行此操作

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

您必须在循环之前初始化xxyy

xx = [];
yy = [];

答案 2 :(得分: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]')