1
votes

The result of solving a polynom equation is a 1x2 vector or a 1x1 in some instances. I am trying to store all solutions for equations with different coefficients. so some solutions are just 1x1 vectors. how can i store these efficiently?

n = 1;
%sol = zeros(size(coef));  %create solution matrix in memory
sol = {};

while n < size(coef,2)
        sol(n) = roots(coef(:,n));

end

"Conversion to cell from double is not possible." error.

coef is coefficient matrix

1

1 Answers

1
votes

You're almost there!

In order to store the vectors as cells in the cell array, use curly braces {} during their assignment:

sol(n) = {roots(coef(:,n))};

or alternatively:

sol{n} = roots(coef(:,n));

That way, the vectors/arrays can be of any size. Check this link for more info about accessing data in cell arrays.

Also, don't forget to increment n otherwise you will get an infinite loop.

Whole code:

n = 1;
%sol = zeros(size(coef));  %create solution matrix in memory
sol = {};

while n <= size(coef,2)
        sol(n) = {roots(coef(:,n))};
n = n+1
end