将嵌套的结构数组重塑为具有大小不同的元素的单元格数组 [英] Reshaping nested struct arrays to cell array having elements with different sizes
问题描述
I have a similar question to my previous one. This time the form of the nested structure looks like this:
Sizes = [2, 5, 8, 6, 3];
cells = 5;
for i = 1:cells
for j = 1:Sizes(i)
a(i).b.c(j).d = rand(1,1);
end
a(i).b.Size = Sizes(i);
end
同样,我想将a(:).b.c(:)
的所有d
值放入包含1 x cells
单元格的单个cell array
中.
Again I would like to put all the d
values of a(:).b.c(:)
into a single cell array
that contains 1 x cells
cells.
这是我使用cellfun
的解决方案,但我想避免使用此功能:
Here is my solution using cellfun
but I would like to avoid this function:
ab = [a.b];
abc = {ab.c};
abcd = cellfun(@(x) [x.d], abc, 'UniformOutput', false);
对abc
使用以前的解决方案:
Using the previous solution for abc
:
abc = [ab.c];
使用字段d
创建一个1x24
结构数组.我曾考虑过使用Size
字段将结果重塑为cell array
,但我不知道如何或是否有可能.如果没有使用循环并且没有cellfun
,您是否有更好的方法?
creates a 1x24
struct array with field d
. I thought of using the Size
field to reshape this result into a cell array
but I don't know how or if it is possible. Do you have a better appraoch without using loops and without cellfun
?
推荐答案
You can do this using mat2cell
as follows:
ab = [a.b];
abc = [ab.c];
abcd = mat2cell([abc.d], 1, [ab.Size]);
这篇关于将嵌套的结构数组重塑为具有大小不同的元素的单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!