我有一个结构向量,每个结构都有一个字段x
:
s1.x = 1;
s2.x = 2;
s3.x = 3;
S = [s1, s2, s3];
我想设置字段x
中所有结构的S
从给定向量X
,即我想对以下循环进行矢量化:
X = [97, 98, 99];
for i = 1 : length(S)
S(i).x = X(i);
end
这可能吗?
你可以这样做:
Xc = num2cell(X); %// convert X to cell array of numbers
[S.x] = Xc{:}; %// generate comma-separated list from cell array, and assign
对于 7.0 之前的 Matlab 版本第二行应该改变 http://es.mathworks.com/help/matlab/ref/deal.html into
[S.x] = deal(Xc{:}); %// generate comma-separated list from cell array, and assign
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)