Using count with negatives

5 ビュー (過去 30 日間)
Jessica Parry
Jessica Parry 2013 年 4 月 10 日
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 件のコメント
Jessica Parry
Jessica Parry 2013 年 4 月 10 日
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 2013 年 4 月 10 日
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?

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2013 年 4 月 10 日
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.

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by