Effacer les filtres
Effacer les filtres

Count/find/sum the number of statements to calculate percentage

1 vue (au cours des 30 derniers jours)
Stuart
Stuart le 17 Avr 2012
Hi,
Using a loop to solve several quadratic equations, my script calculates tells me whether 'The equation is good' or 'The equation has no real roots'.
These statements are printed using fprintf. In the form:
The equation is good
The equation is good
The equation has no real roots
The equation is good...etc
Is there an easy way to calculate how many times 'The equation is good' is printed?
I've tried to use sum(fprintf== 'The equation has no real roots') but it doesn't seem to work.
Thanks

Réponse acceptée

Wayne King
Wayne King le 17 Avr 2012
You can always include a counter variable in the loop
k = 0; % outside the loop, set k to 0
if (your real roots test)
fprintf('The equation has no real roots\n');
k = k+1;
end
sum(k)

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 17 Avr 2012
eg
ii = 0;
for ...
...
if all(abs(imag(roots) < 100*eps))
fprintf('The equation is good');
ii = ii + 1;
end
...
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by