Divide table takes a lot of calculation time

4 ビュー (過去 30 日間)
Jesse
Jesse 2018 年 11 月 15 日
コメント済み: Guillaume 2018 年 11 月 16 日
In my script i want to devide a table, it works for now, but it takes a lot of time.
How can i do this easier or faster?
reactionforce{3:end,:} = reactionforce{3:end,:}/1e10

採用された回答

Guillaume
Guillaume 2018 年 11 月 15 日
I doubt you're using any feature of a table with a table that large so why not store your data in a matrix instead. Matrix operations will be slightly faster. Saying that, with your example table, the division takes 76 milliseconds on my computer. I would hardly call that a lot of time.
reactionforce = table2array(reactionforce); %convert table to matrix since you don't use the features of tables
%...
reactionforce(3:end, :) = reactionforce(3:end, :) / 1e10;
  3 件のコメント
Jesse
Jesse 2018 年 11 月 16 日
Thanks, on my computer it took about 20 seconds, so maybe that could also be the problem ;)
With this method it is much faster, about 1 second.
Thanks!
Guillaume
Guillaume 2018 年 11 月 16 日
The contents of a table is always held as a cell array, with one table variable (column) per cell. Therefore, when you write reactionforce{3:end, :} = reactionforce{3;end, :}/1e10, matlab has to:
  • concatenate all these cells into one big matrix. This involves copying the content of each variable into a new memory location
  • extract the rows of the matrix that you asked for
  • perform the division
  • convert the result back into a cell array of column
So this is always going to take more time than just performing a division on a matrix. It's surprising that it's so slow on your machine though.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by