将嵌套的结构数组重塑为具有大小不同的元素的单元格数组 [英] Reshaping nested struct array to cell array having elements with different sizes
问题描述
在MATLAB中,我具有以下形式的struct数组:
In MATLAB, I have a struct array of the following form:
a(1).b.c = rand(1,10);
a(1).b.cSize = length(a(1).b.c);
a(2).b.c = rand(1,11);
a(2).b.cSize = length(a(2).b.c);
a(3).b.c = rand(1,12);
a(3).b.cSize = length(a(3).b.c);
a(4).b.c = rand(1,13);
a(4).b.cSize = length(a(4).b.c);
a(5).b.c = rand(1,14);
a(5).b.cSize = length(a(5).b.c);
a(6).b.c = rand(1,15);
a(6).b.cSize = length(a(6).b.c);
我想创建一个单元格数组c
,其中包含嵌套结构的不同大小的字段a.b.c
,而无需使用for循环.
I would like to create a cell array c
that contains the differently sized fields a.b.c
of the nested struct, without using for loops.
我尝试了以下操作:
c = {a.b.c}
不起作用,并返回以下错误消息:
which is not working and returns the following error message:
期望大括号或点索引表达式的一个输出, 但是有6个结果.
Expected one output from a curly brace or dot indexing expression, but there were 6 results.
到目前为止,我发现的最佳解决方案是
The best solution I've found so far is the following
cellfun(@(x) x.c, {a.b}, 'UniformOutput', false)
有没有使用cellfun
的更快的解决方案?也许有些reshape
命令?
Is there a faster solution without using cellfun
? Maybe some reshape
command?
推荐答案
您可以从a.b
创建一个structrue数组,然后从该数组中提取字段c
.
You can create a structrue array from a.b
then extract the field c
from the array.
ab = [a.b];
result = {ab.c}
这篇关于将嵌套的结构数组重塑为具有大小不同的元素的单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!