以矢量化方式从非标量结构数组中的元素中减去常量值
Subtract a constant value from elements in a non-scalar struct array in a vectorized way
我有这样的结构
temp_struct(1).budget=8
temp_struct(2).budget=8
我想从它们中减去一个常数值(用新值替换 8)。如何在不使用 matlab 循环的情况下更有效地做到这一点?
%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{:}
我有这样的结构
temp_struct(1).budget=8
temp_struct(2).budget=8
我想从它们中减去一个常数值(用新值替换 8)。如何在不使用 matlab 循环的情况下更有效地做到这一点?
%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{:}