Octave:创建和初始化结构的简洁方法
Octave: concise way of creating and initializing a struct
我有一个长度为 3 的字符串元胞数组
headers_ca =
{
[1,1] = time
[1,2] = x
[1,3] = y
}
我想创建一个模仿 python 字典的 struct
,将 headers_ca
中的值作为键(Octave 中的字段名)和初始化值 ival
对于所有条目。
它将是 struct
,因为即使 dict
也存在于八度音阶中,it has been deprecated.
我可以(蛮力)s = struct("time", ival, "x", ival, "y", ival);
最简洁的方法是什么?
我知道我可以做一个 for 循环。
能避免吗?
我会使用更长的元胞数组。
您可以使用 struct or cell2struct 创建结构。
headers_ca = {'time','x','y'};
headers_ca(2, :) = {ival};
s = struct(headers_ca{:});
headers_ca = {'time','x','y'};
ivals = repmat({ival}, numel(headers_ca), 1);
s = cell2struct(ivals, headers_ca);
我有一个长度为 3 的字符串元胞数组
headers_ca =
{
[1,1] = time
[1,2] = x
[1,3] = y
}
我想创建一个模仿 python 字典的 struct
,将 headers_ca
中的值作为键(Octave 中的字段名)和初始化值 ival
对于所有条目。
它将是 struct
,因为即使 dict
也存在于八度音阶中,it has been deprecated.
我可以(蛮力)s = struct("time", ival, "x", ival, "y", ival);
最简洁的方法是什么?
我知道我可以做一个 for 循环。 能避免吗?
我会使用更长的元胞数组。
您可以使用 struct or cell2struct 创建结构。
headers_ca = {'time','x','y'};
headers_ca(2, :) = {ival};
s = struct(headers_ca{:});
headers_ca = {'time','x','y'};
ivals = repmat({ival}, numel(headers_ca), 1);
s = cell2struct(ivals, headers_ca);