MATLAB: How to use findpeaks to obtain the peaks of a vector, whilst maintaining their position on the axis (instead of compressing all the peak values into a smaller vector)

findpeakspeak location

Hi there
Every time I use findpeaks it returns a vector that is much smaller than the starting vector (that contains the peaks). I want to find the peaks in a vector that is 10033×1, but then plot those peaks against another vector (also 10033×1) whilst maintaining the scale. I have tried multiple things to return a vector of the same size, but nothing seems to work. Here the most recent example of what I have tried:
x = linspace(0, max(AhAh), :);
%Finding Charge Capacity
[Cpks, locs] = findpeaks(AhAh, x);
%Finding Discharge Capacity
[Dpks, locs] = findpeaks(-AhAh, x);
– AhAh is 10033×1
– UV is 10033×1
– I want to plot the peaks of AhAh against UV
Any help would be greatly appreciated!
Many thanks
Rob

Best Answer

Say you have x1, x2 and y1, y2
[~, locs1] = findpeaks(y1)
[~, locs2] = findpeaks(y2)
Compare by
plot(x1(locs1), y1(locs1),...
x2(locs2), y2(locs2))