cell2mat too slow

11 views (last 30 days)
Douglas Chiang
Douglas Chiang on 29 Jul 2021
Commented: Douglas Chiang on 31 Jul 2021
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

Accepted Answer

Bruno Luong
Bruno Luong on 29 Jul 2021
Try
cat(1, nodes.pose)

More Answers (1)

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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!