cell2mat too slow

18 ビュー (過去 30 日間)
Douglas Chiang
Douglas Chiang 2021 年 7 月 29 日
コメント済み: Douglas Chiang 2021 年 7 月 31 日
Hi community,
I am trying to extract all poses from a struct by:
arrayfun(@(node)(node.pose), nodes, 'UniformOutput', false)
It gives me something like this:
ans =
50×1 cell array
{[ -0.2232 6.2129 0.8631 -0.5050 0]}
{[-0.5066 5.4770 -0.9729 -0.2310 0]}
{[ -0.2650 6.0300 0.7742 -0.6329 0]}
{[ -0.0436 5.4497 0.7313 -0.6820 0]}
{[ -0.0892 5.5517 -0.4583 0.8888 0]}
{[ 0.4760 6.0669 -0.3153 -0.9490 0]}
{[ -0.5377 6.2957 0.9279 -0.3729 0]}
{[-0.6971 5.9071 -0.7730 -0.6344 0]}
...
But I want it to be a matrix like this:
ans =
-0.2232 6.2129 0.8631 -0.5050 0
-0.5066 5.4770 -0.9729 -0.2310 0
-0.2650 6.0300 0.7742 -0.6329 0
-0.0436 5.4497 0.7313 -0.6820 0
-0.0892 5.5517 -0.4583 0.8888 0
0.4760 6.0669 -0.3153 -0.9490 0
-0.5377 6.2957 0.9279 -0.3729 0
-0.6971 5.9071 -0.7730 -0.6344 0
...
So I use cell2mat:
cell2mat(arrayfun(@(node)(node.pose), nodes, 'UniformOutput', false))
But the function cell2mat is spending around 50% of runtime for my program, which slow down quite a lot.
Is there anyway to avoid cell2mat that has an efficient runtime?
Thanks

採用された回答

Bruno Luong
Bruno Luong 2021 年 7 月 29 日
Try
cat(1, nodes.pose)
  2 件のコメント
Douglas Chiang
Douglas Chiang 2021 年 7 月 31 日
Thanks, this is much faster than cell2mat()

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

その他の回答 (1 件)

Yazan
Yazan 2021 年 7 月 29 日
To speed up the computations, you simply need to avoid using structure arrays.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by