matlab concatenate vectors if cycle

3 views (last 30 days)
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

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 4 Oct 2012
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 Comment
joo
joo on 4 Oct 2012
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!