将嵌套的结构数组重塑为具有大小不同的元素的单元格数组 [英] Reshaping nested struct arrays to cell array having elements with different sizes

查看:77
本文介绍了将嵌套的结构数组重塑为具有大小不同的元素的单元格数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆