matlab concatenate vectors if cycle

1 次查看(过去 30 天)
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
hello
i need to construct vectors xx and yy. The problem is that at each cycle the past xx and yy is deleted but i want the opposite. i want them to keep the past information and grow at each cycle. what can i do? and also i know i should preallocate xx and yy.
i appreciate any help. thank you very much.
**this is a possible solution. the problem here is i need to preallocate. but if i do it, the xx and yy keep the zeros and continue to grow 'with the zeros inside' and that is wrong
:
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

采纳的回答

Titus Edelhofer
Titus Edelhofer 2012-10-4
Hi,
to grow the xx and yy you would write the following:
xx = [xx x(iPeaks1:iPeaks2)];
or
xx = [xx; x(iPeaks1:iPeaks2)];
depending on x being a row vector (first version) or a column vector (second). If you want to preallocate:
N = length(iPeaksNoise);
xx = zeros(N, 1);
yy = zeros(N,1);
counter = 0;
for i = 1 : length(iPeaksNoise)-1
iPeaks1 = iPeaksNoise(i);
iPeaks2 = iPeaks(i+1)-1;%analyse of consecutive pair of peaks
%skip small noise peaks
nPeaks = iPeaks2-iPeaks+1;
if nPeaks>=5
xx(counter+(1:nPeaks)) = x(iPeaks1:iPeaks2);
yy(counter+(1:nPeaks)) = y(iPeaks1:iPeaks2);
counter = counter + nPeaks;
end
end
xx(counter+1:end) = [];
yy(counter+1:end) = [];
Titus
  1 个评论
joo
joo 2012-10-4
can you please see the new code i wrote in the 'question space'. do you know what i could do to solve the problem? thank you so much.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by