以矢量化方式从非标量结构数组中的元素中减去常量值

时间:2015-03-14 14:03:05

标签: performance matlab vectorization

我有这样的结构

temp_struct(1).budget=8
temp_struct(2).budget=8

我希望从它们中减去一个常量值(用新值替换8)。如果不在matlab中使用循环,我怎样才能更有效地做到这一点?

1 个答案:

答案 0 :(得分:4)

%extract a cs list and convert it to a vector, then apply the operation you want in a vectorized manner
a=[temp_struct(:).budget]-42
%convert to cell because there is no direct way from vector to cs list
a=num2cell(a)
%use a cs list to assign the values.
[temp_struct(:).budget]=a{:}

What is a cs list?