matlab concatenate vectors if cycle
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Titus Edelhofer
am 4 Okt. 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!