Using count with negatives
5 views (last 30 days)
Show older comments
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 Comments
Image Analyst
on 10 Apr 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?
Answers (1)
Jan
on 10 Apr 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.
0 Comments
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!