Using find and allocating answer to an array

1 visualizzazione (ultimi 30 giorni)
Rock
Rock il 13 Lug 2012
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?

Risposte (2)

Sean de Wolski
Sean de Wolski il 13 Lug 2012
Modificato: Sean de Wolski il 13 Lug 2012
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 il 13 Lug 2012
Modificato: nanren888 il 13 Lug 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 Commenti
nanren888
nanren888 il 13 Lug 2012
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.
nanren888
nanren888 il 13 Lug 2012
x = linspace(0,5,1001);
s = sin(2*pi*1*x);
[p,l] = findpeaks(s);
plot(x,s,x(l),p,'O');

Accedi per commentare.

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by