如何在 MATLAB 中连接属于元胞数组一部分的元胞数组?

发布于 2024-11-08 08:05:38 字数 1121 浏览 0 评论 0原文

我有一个元胞数组 allData ,它是 Nx1。每个单元格都包含一个带有 names 属性的结构(名称是一个自定义对象,但如果您愿意,可以将其视为字符串单元格数组)。我想创建一个包含所有名称的单个元胞数组。例如,如果 N=3,那么 allData 是一个 3x1 元胞数组,那么以下方法将实现我的目标:

A = allData{1};
B = allData{2};
C = allData{3};

allNames = [A.names B.names C.names];

这种方法的问题是 N 很大并且会根据输入而变化,因此我希望有一种聪明的方法可以使用 cellfun 来做到这一点,但是我尝试过的所有方法都失败了(例如,执行 allNames = [cellfun(@( x) {x.names}, allData)];)。

更新:感谢建议的解决方案,我可以将所有内容放入一个元胞数组中,其中每个元胞包含一个元胞数组。我的目标是将这些连接起来。或多或少,我拥有的是:

{A.names B.names C.names} 

而我想要但似乎无法得到的是

{A.names{1} A.names{2} ... A.names{end} B.names{1} ... B.names{end} ...}

解决方案: 我需要下面每个答案的一部分,所以这是对我有用的解决方案:

来自 Andrew Lazarus

allNames = arrayfun(@(x) x.name, [allData{:}], 'UniformOutput', false);

然后,从 gnovice

allNames = vertcat(allNames{:});

感谢两位!!

I have a cell array allData which is Nx1. Each cell contains a structure with a names property (the name is a custom object, but think of it as a cell array of strings if you like). I would like to create a single cell array that contains all of the names. For example, if N=3, so that allData is a 3x1 cell array, then the following would accomplish my goal:

A = allData{1};
B = allData{2};
C = allData{3};

allNames = [A.names B.names C.names];

The problem with this approach is that N is large and changes depending on the input, so I'm hoping that there is a clever way to do this using cellfun, but everything that I've tried fails (e.g. it doesn't work to do allNames = [cellfun(@(x) {x.names}, allData)];).

UPDATE: Thanks to the suggested solutions, I can get everything into one cell array where each cell contains a cell array. My goal here is to concatenate these. More or less, what I have is:

{A.names B.names C.names} 

and what I want and cannot seem to get is

{A.names{1} A.names{2} ... A.names{end} B.names{1} ... B.names{end} ...}

SOLUTION:
I needed pieces of each answer below, so here's the solution that worked for me:

from Andrew Lazarus:

allNames = arrayfun(@(x) x.name, [allData{:}], 'UniformOutput', false);

then, from gnovice:

allNames = vertcat(allNames{:});

Thanks to both!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-11-15 08:05:39

对于更通用的解决方案,需要更多有关单元格内容的详细信息,但是如果 names 属性始终返回字符串单元格数组,并且如果您的结构都是标量(即 1 -by-1 结构数组),然后使用 CELLFUN 的以下解决方案, <一href="http://www.mathworks.com/help/techdoc/ref/char.html" rel="nofollow">CHAR 和 CELLSTR 将为您提供一个包含您所有姓名的 N×1 字符串元胞数组 allNames

allNames = cellfun(@(x) {char(x.names)},allData);
allNames = cellstr(char(allNames{:}));

并且这是一个例子allData 包含三种不同的结构:

>> allData{1} = struct('names',{{'hello'}},'junk',1:3);
>> allData{2} = struct('names',{{'hi' 'yo' 'hey' 'whassup'}});
>> allData{3} = struct('names',{{'howdy';'aloha'}},'junk',4);
>> allNames = cellfun(@(x) {char(x.names)},allData);
>> allNames = cellstr(char(allNames{:}))

allNames = 

    'hello'
    'hi'
    'yo'
    'hey'
    'whassup'
    'howdy'
    'aloha'

编辑:

概括为 names 属性返回对象元胞数组的情况,不一定是字符串,您可以尝试这个解决方案,重塑每个元胞数组进入 M×1 元胞数组,然后垂直连接将它们全部连接成一个 N×1 元胞数组对象:

allNames = cellfun(@(x) {reshape(x.names,[],1)},allData);
allNames = vertcat(allNames{:});

或者,如果您希望最终得到一个 1×N 对象元胞数组,您可以这样做:

allNames = cellfun(@(x) {reshape(x.names,1,[])},allData);
allNames = [allNames{:}];

There are a few more details about your cell contents that would be needed for a more general solution, but if the names property always returns a cell array of strings, and if your structures are all scalars (i.e. 1-by-1 structure arrays), then the following solution using CELLFUN, CHAR, and CELLSTR will give you an N-by-1 cell array of strings allNames containing all of your names:

allNames = cellfun(@(x) {char(x.names)},allData);
allNames = cellstr(char(allNames{:}));

And here's an example where allData contains three different structures:

>> allData{1} = struct('names',{{'hello'}},'junk',1:3);
>> allData{2} = struct('names',{{'hi' 'yo' 'hey' 'whassup'}});
>> allData{3} = struct('names',{{'howdy';'aloha'}},'junk',4);
>> allNames = cellfun(@(x) {char(x.names)},allData);
>> allNames = cellstr(char(allNames{:}))

allNames = 

    'hello'
    'hi'
    'yo'
    'hey'
    'whassup'
    'howdy'
    'aloha'

EDIT:

Generalizing to the case where the names property returns a cell array of objects, not necessarily strings, you can try this solution which reshapes each cell array into an M-by-1 cell array, then vertically concatenates all of them into an N-by-1 cell array of objects:

allNames = cellfun(@(x) {reshape(x.names,[],1)},allData);
allNames = vertcat(allNames{:});

Or, if you would rather end up with a 1-by-N cell array of objects, you can do this:

allNames = cellfun(@(x) {reshape(x.names,1,[])},allData);
allNames = [allNames{:}];
灼疼热情 2024-11-15 08:05:39

试试这个

 allNames = arrayfun(@(x) x.name, [allData{:}], 'UniformOutput', false)

省略 UniformOutput 可变参数以进行直接串联。这给出了单元输出。

Try this

 allNames = arrayfun(@(x) x.name, [allData{:}], 'UniformOutput', false)

Omit the UniformOutput vararg for straight concatenation. This gives a cell output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文