How can i use roots function with a 100x3 matrix

I have a nx3 matrix. n rows and 3 columns. Every elements in a row are coefficents of a polynomial.
I want to get roots for every row.
After i get roots i want to eleminate nonlogical root and i can get logical root which i need.
Any idea of this?

 Réponse acceptée

result = cellfun(@roots, num2cell(yourmatrix, 2), 'UniformOutput', false)
Will return a nx1 cell array of column vectors which are the roots of every row.
Alternatively, do it with a loop:
result = cell(size(yourmatrix, 1), 1);
for row = 1:size(yourmatrix, 1)
result{row} = roots(yourmatrix(row, :));
end
I have no idea what a logical or nonlogical root is.

4 commentaires

Gko K
Gko K le 24 Mar 2019
Thank you friend. That works.
This matrix is 2nd degree polynomial. So there are 2 roots. Roots can be in 2 conditions for my problem:
Condition 1:
1 negative and 1 positive
Condition 2:
2 positive(1 root is smaller, 1 roos is very bigger)
I mean with logical for Condition 1-the positive root and for Condition 2-not the bigger root.
How can i get the true root for my problem?
Then use the loop. If you only keep one root, then you don't even need a cell array:
result = zeros(size(yourmatrix, 1), 1);
for row = 1:size(yourmatrix, 1)
rowroots = roots(yourmatrix(row, :));
if any(rowroots < 0)
result(row) = rowroots(rowroots >= 0);
else
[~, tokeep] = min(abs(rowroots));
result(row) = rowroots(tokeep);
end
end
Gko K
Gko K le 25 Mar 2019
Thank you i will try that
Gko K
Gko K le 25 Mar 2019
Thank you friend, that works :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2018b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by