For loop keeps repeating same value

I am trying to write a code that finds all maximum values above threshold. What I am trying to do is find the maximum value, make it zero or remove it, find the next maximum value until all maximum values above threshold are identified. But, pdpulse(n,:) = c keeps repeating the same value which the first maximum value in x. I think the problem comes from the loops but I can't figure out why and how to fix it.
x = Data(:, 1); % import Y of column 1 from Data
for n = 1:2000
[M,I] = max (x);
if M > 2
c = x(I-299 : I+700); % range of maximum values i.e. pre and post processing
c = c'; % transpose
pulse(n,:)= c; % n-th row of pulse
end
M = 0;
end
Thank you

Risposte (1)

Walter Roberson
Walter Roberson il 21 Nov 2016

0 voti

Your c = x(I-299 : I+700) does not depend upon your loop variable n and does not depend upon any value that is changing in your loop, so it is always going to have the same result.

7 Commenti

Safwan
Safwan il 21 Nov 2016
Modificato: Safwan il 21 Nov 2016
Shouldn't the value of c change with I which changes with M? So I assume the value of M does not change hence the repeating values.
To repeat what Walter said, but with a bit more specificity ...
In this line of code,
[M,I] = max (x);
there is no dependence on n, your looping variable, so exactly the same thing will be done every time. M does not actually change.
Thank you for enlightening me. I thought M = 0 would be enough to change the M every time. Do you have any suggestion on how to include n into the loop?
Maybe you intended this line
c = x(I-299 : I+700)
to have dependence on n, and not just I?
Or perhaps you forgot a step of changing x(I) to some value that cannot be the maximum (e.g., nan or -inf)
This is actually pretty dumb but I don't quite understand how this line works but I know it provides me the range I want for one maximum value
c = x(I-299 : I+700)
That code is not finding values above a threshold: it is finding values in a window 1000 wide near the current maximum. It could fail if the location of the current maximum is within 299 of the beginning or within 700 of the end. If you were iterating setting maxima to no longer be considered, you would run the risk that your flag value (e.g., -inf or nan) would be within the range as well.
If you want to find all maximum values over a threshold then,
x(x >= TheThreshold)
would give you that. If you wanted them in decreasing order, then sort() with 'descend'

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 21 Nov 2016

Commentato:

il 23 Nov 2016

Community Treasure Hunt

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

Start Hunting!

Translated by