Improving the speed of code with passing to a function input a cell array or structure.

I'm trying to pass some amount of numerical data as a function input.
I had that row in my code:
[z1,y1]=ode45(@(z,y) famplifire(sigma_pa,N0,sigma_pe,sigma_se,k,y,z),zspan,startval);
I'm trying to replace four parameters: sigma_pa,N0,sigma_pe,sigma_se. I've tryed to replace them with cell array, but the time of execution of code nevertheless rises. So I had some questions:
1) Using of cell arrays insted of usual numerical data can improve quickness of my code?
2) I had another idea to use a struct, but I don't know how. If you can, give me some advices (or simple examples) please. Or I can make another question with my code.

 採用された回答

Matt J
Matt J 2021 年 4 月 3 日
編集済み: Matt J 2021 年 4 月 3 日

0 投票

We can't see what famplifire [sic.] is doing with your variables so there isn't any specific comments that can be made. However, the rule of thumb is basically that if you must loop through a small number of large arrays/matrices, it can be advantageous to hold them in cells. Otherwise, it is probably better to concatenate them into a single array.
Structures are essentially the same as cell arrays. They are just indexed differently.

3 件のコメント

George Bashkatov
George Bashkatov 2021 年 4 月 3 日
編集済み: George Bashkatov 2021 年 4 月 3 日
So, famplifire solve two ODE'S. Here it is:
function dydz = famplifire1(C,k,y,z)
k=C{2}*C{1}/(C{1}+C{3});
dy1 = y(1).*(-C{1}.*C{2}+(C{1}+C{3}).*(k.*y(1)./(y(1)+y(2)+1)));
dy2 = y(2).*C{4}.*(k.*y(1)./(y(1)+y(2)+1));
dydz = [dy1; dy2];
I've changed all constants to C{i}, i=1..4.
Matt J
Matt J 2021 年 4 月 3 日
If the C{i} are all scalars, it would be better if C were numeric rather than a cell array
George Bashkatov
George Bashkatov 2021 年 4 月 4 日
Thank you a lot

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by