Using count with negatives

5 vues (au cours des 30 derniers jours)
Jessica Parry
Jessica Parry le 10 Avr 2013
I've written a code (as below), and now i'm trying to count the values of S_coeff_fact, however it won't work for the negative numbers?! I don't know why as ive never had this problem before.
function [S, R] = poly_fact(P, Q)
sizeP = size(P); rowsP = sizeP(2);
sizeQ = size(Q); rowsQ = sizeQ(2);
diff_in_length = rowsP - rowsQ;
i = diff_in_length+1;
count = 0;
while i>0
i = i-1;
count = count+1;
zero_input = zeros(1,diff_in_length);
Q = [Q zero_input];
S_coeff_fact = P(1, 1)/Q(1,1);
Q = Q*S_coeff_fact
P = P - Q
P = P(find(P ~= 0)); % Removing zeros from P.
Q = Q(find(Q ~= 0)); % Removing zeros from Q.
sizeP = size(P); rowsP = sizeP(2);
sizeQ = size(Q); rowsQ = sizeQ(2);
diff_in_length = rowsP - rowsQ;
S(count) = count(S_coeff_fact)
end
S = S
R = P
end
  2 commentaires
Jessica Parry
Jessica Parry le 10 Avr 2013
The error is
Attempted to access count(-7); index must be a positive integer or logical.
Error in poly_fact (line 47)
S(count) = count(S_coeff_fact)
Please help, thanks!
Image Analyst
Image Analyst le 10 Avr 2013
What does " count the values of S_coeff_fact" mean? Do you mean length(S_coeff_fact), or sum(S_coeff_fact)??? Can you put some comments in to explain the code (actually that might help you realize a logic error)? Can you supply some small sample arrays for P and Q so we can try it?

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 10 Avr 2013
Your variable count is a scalar, such that indexing it with count(S_coeff_fact) must fail for anything but S_coeff_fact == 1. Therefore I claim, that this code did not work properly ever.

Catégories

En savoir plus sur Polynomials 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