필터 지우기
필터 지우기

matlab concatenate vectors if cycle

조회 수: 1 (최근 30일)
joo
joo 2012년 10월 4일
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