Using find and allocating answer to an array

I would like to use the find command to give me the locations of certain peaks in a signal. I have the amplitudes of the peaks I just have to find their locations.
Here is my code:
for n = 1:length(acc);
totalPlocations(n) = find(data==peakamp(n))
end
--where peakamp is the the amplitudes of the peaks.
and i get the error
In an assignment A(I) = B, the number of elements in B and I must be the same.
The problem is that some, but not all of the amplitudes are equal, so when i find the locations it gives me a different size array for each loop. If all the amplitudes were different, then my code would work just fine. How do I remedy this?

Answers (2)

Well, one way would be to just find one value
find(x,1,'first')
or you could use a cell array to store as many results as you get:
totalPlocations = cell(length(acc),1);
for ii =...
totalPlocations{ii} = find(...
end
nanren888
nanren888 on 13 Jul 2012
Edited: nanren888 on 13 Jul 2012
Sorry, I don't get where "acc" comes into it.
You could try
totalPlocations = zeros(size(peakamp));
peakamp = unique(peakamp);
nextI = 1;
for n = 1:length(peakamp)
tmpIndices = find(data==peakamp(n));
nCount = length(tmpIndices);
totalPlocations(nextI-1+(1:nCount)) = tmpIndices;
nextI = nextI + nCount;
end
If the peak value appears in the array more than once, you need to make sure the associated peaks don't get added more than once. The 'first' solution will, in that case, add the first one twice.
or if there are not too many peaks
totalPlocations = zeros(size(peakamp));
peakamp = unique(peakamp);
for n = 1:length(peakamp)
totalPlocations = [totalPlocations find(data==peakamp(n))];
end
this will get you a warning about the array growing, which is slow for large arrays.
It sort of depends whether you need the peak locations in any particular order?
BTW: (1) I am intrigued how you have the magnitudes without the locations. Most things I have written, can recover the locations when they find the peaks.
(2) Generally, comparing floating point numbers with "==" is a risky thing to do, yes, even with modern IEEE maths & such.

3 Comments

Rock
Rock on 13 Jul 2012
Edited: Rock on 13 Jul 2012
sorry the acc should say peakamp so that the first line would say for n = 1:length(peakamp) (1) well I used the findpeaks command to find the magnitudes of the peaks but with findpeaks the locations of the peaks is in the new range of points and not the locations of the peaks in the original data. But thanks, I think this will get my code to work.
Glad to have helped.
Frankly, I think you should rethink your method. The locations must be recoverable from finding the peaks. And to harp on, using "==" with floating point values is not the best idea.
x = linspace(0,5,1001);
s = sin(2*pi*1*x);
[p,l] = findpeaks(s);
plot(x,s,x(l),p,'O');

Sign in to comment.

Products

Tags

Asked:

on 13 Jul 2012

Community Treasure Hunt

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

Start Hunting!