フィルターのクリア

Vectorize a code that finds the roots of polynomials stored in matrices

4 ビュー (過去 30 日間)
Naveen
Naveen 2013 年 2 月 6 日
I need some tips on how to vectorize a code that finds the roots of polynomials stored in matrices. What I have is a multidimensional array that contains 100 matrices of 100-by-3 size. I'm pasting the code below
one = ones ( 100, 100 );
poly.struc = reshape( [ one; gr_fac1; - gr_fac2 ], 100, 3, [] );
poly_roots_select = zeros ( 100, 100);
for j = 1:1:100
poly_matrices = poly.struc ( :, :, j );
for i = 1:1:100
poly_select = poly_matrices ( i, : );
poly_roots = roots ( poly_select );
poly_roots_select ( i , j) = poly_roots ( real ( poly_roots) > 0 , 1 ) ;
end
end
I would like to remove both for loops. At minimum I like to be able to access the matrices stored in poly.struc array without putting a loop for this. Is there also any other way to be able to pass a row vectors from a matrix to roots function without needing a loop?
Thanks in advance

採用された回答

Thorsten
Thorsten 2013 年 2 月 7 日
The roots function only accepts vectors.
You may speed up your code by getting rid of the two intermediate variables poly_matrices and poly_select:
for j = 1:1:100
for i = 1:1:100
poly_roots = roots ( poly.struc ( i, :, j ) );
poly_roots_select ( i , j ) = poly_roots ( real ( poly_roots) > 0 , 1 ) ;
end
end
  2 件のコメント
Naveen
Naveen 2013 年 2 月 7 日
Thanks for your suggestions! So you think that in this program, it's not possible to remove for loops? I was thinking to get rid of at least one for loop by passing this poly.struc as an argument to a function which also accept index j as other argument. Before I make a call to the function, I can vectorize j and hopefully function would take the vectored j as an argument. Inside this function, I can use one for loop to calculate the roots. What do you think?
Thorsten
Thorsten 2013 年 2 月 7 日
If your find a vectorized version of the roots function this may work.

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

その他の回答 (0 件)

カテゴリ

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