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

Performance 以矢量化方式从非标量结构数组中的元素中减去常量值,performance,matlab,vectorization,Performance,Matlab,Vectorization,我有一个这样的结构 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 %conve

我有一个这样的结构

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{:}

交易是不必要的,在matlab 2013a中测试这比for循环快吗?我得到:此方法的运行时间为0.042050秒,而此方法的运行时间为0.003850秒-loop@JustCurious:在最近的matlab版本中,for循环的速度提高了,如果循环更快,我也不会感到惊讶。