gpt4 book ai didi

matlab - 将结构数组转换为结构单元,反之亦然

转载 作者:行者123 更新时间:2023-12-02 03:44:10 25 4
gpt4 key购买 nike

我经常需要处理包含具有相同字段名的标量结构的结构数组和单元格,我称之为“解压结构数组”,我想知道 Matlab 和/或 GNU Octave 中是否还没有函数可以帮助它们之间的转换两种表示。

结构数组:

foo(1).a = 3;
foo(1).b = pi;
foo(2).a = 5;
foo(2).b = 2.718;

显然 num2cell 在 GNU Octave ( although it isn't mentioned in the docs ) 中以一种方式工作:

ret = num2cell (foo)
ret =
{
[1,1] =

scalar structure containing the fields:

a = 3
b = 3.1416

[1,2] =

scalar structure containing the fields:

a = 5
b = 2.7180

}

但我正在寻找相反的部分,将 ret 转换回 foo

最佳答案

这似乎可以满足您的要求:

foo2 = [ret{:}]; % or equivalently foo2 = horzcat(ret{:});

也就是说,只是水平连接元胞数组的内容。

检查:

>> foo(1).a = 3;
foo(1).b = pi;
foo(2).a = 5;
foo(2).b = 2.718;
>> ret = num2cell (foo);
>> foo2 = [ret{:}];
>> isequal(foo, foo2)
ans =
logical
1

关于matlab - 将结构数组转换为结构单元,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47351736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com