How to store some varialbes from a very large loop?
1 view (last 30 days)
Show older comments
Hello,
I am sorry if this is a dumb question, I am not a very experienced programmer. All help is greatly appreciated.
I am trying to find the local maxima of a complex system. I have an array pattern (theta, dB) with multiple peaks. I have used findpeaks in the past to do this, however my values are now complex numbers and findpeaks does not support this. I am currently using a simple code to find this, which is shown below, but the way I am storing my values returns a matrix the size of the loop, with the values I want from the if statement, and the rest of the values are zero.
for n = 1:size(ArrayPattern_dB)-1
if ArrayPattern_dB(n)> ArrayPattern_dB(n+1) && ArrayPattern_dB(n)>ArrayPattern_dB(n-1);
disp(ArrayPattern_dB(n))
disp(theta_deg(n))
A(n) = ArrayPattern_dB(n);
B(n) = theta_deg(n);
Peaks = [B A];
end
end
I would like to just return a matrix that contains the value and location of the peaks, but I cannot think of a way to do this.
Thank you, Jason
1 Comment
Youssef Khmou
on 21 Apr 2014
First proposition : use the absolute value of the pattern (abs(.)) then apply the algorithm for peak detection,
Accepted Answer
Ken Atwell
on 21 Apr 2014
If you initialize 'A' and 'B' as not-a-number (NAN) vectors, you could delete the NANs after the loop using logical indexing.
Before the loop:
A = nan(size(Array_Pattern));
B = nan(size(Array_Pattern));
After the loop
A = A(~isnan(A));
B = B(~isnan(B));
By the way, I believe your 'for' loop needs a little work, and you want to begin at '2' and not '1'. MATLAB indexes begin with one, and accessing the zero'th element of an array is an error. If you begin your loop at 1, 'ArrayPattern_dB(n-1)' will trigger an error.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!