cell2mat too slow

25 次查看(过去 30 天)
Douglas Chiang
Douglas Chiang 2021-7-29
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)

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by