Using find and allocating answer to an array

조회 수: 1 (최근 30일)
Rock
Rock 2012년 7월 13일
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?

답변 (2개)

Sean de Wolski
Sean de Wolski 2012년 7월 13일
편집: Sean de Wolski 2012년 7월 13일
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 2012년 7월 13일
편집: nanren888 2012년 7월 13일
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
nanren888
nanren888 2012년 7월 13일
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 2012년 7월 13일
x = linspace(0,5,1001);
s = sin(2*pi*1*x);
[p,l] = findpeaks(s);
plot(x,s,x(l),p,'O');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by