필터 지우기
필터 지우기

Subtract a constant value from some elements in a struct

조회 수: 7 (최근 30일)
Alex
Alex 2015년 3월 14일
댓글: Alex 2015년 3월 16일
I have a struct like this
temp_struct(1).budget=8
temp_struct(2).budget=8
and I want to subtract a constant value from both of them (replacing 8 with the new value). How can i do it more efficiently without using a loop?

답변 (3개)

Image Analyst
Image Analyst 2015년 3월 14일
I agree with Jan. You can do a loop with tens of millions of iterations in a fraction of a second, so the for loop itself is not a problem.
for k = 1 : length(temp_struct)
temp_struct(k).budget = temp_struct(k).budget - 1;
end
Also, another benefit is that the above code is simpler than something like what Guillaume gave and easier for people to understand and figure out what you did.

Guillaume
Guillaume 2015년 3월 14일
You'll have to use matlab's automatic expension of cell arrays to comma separated list to assign to the elements of your structure array.
temp_struct = struct('budget', {8 10 7 6}, 'otherfield', 5);
subresult = num2cell([temp_struct.budget] - 5); %convert result of subtraction into cell array
[temp_struct.budget] = subresult{:}; %expand cell array to list and assign

Jan
Jan 2015년 3월 14일
편집: Jan 2015년 3월 14일
If you prefer to store the values in fields of a struct, the loop method is the most efficient way to access them. If you need a faster method for calculations, storing the values in an array directly is much better.
Even if the wanted procedure is possible with a something like structfun, this must be performed by a loop internally also, which has search in the list of field names. Therefore a low speed is not a problem of the loop, but of the data representation.
  댓글 수: 3
Jan
Jan 2015년 3월 14일
편집: Jan 2015년 3월 14일
A double speed is no hard argument for a specific piece of code, except that this piece is the bottleneck of the complete program. When the time for programming and debugging is taken into account also, optimizing micro-seconds is not useful.
But of course you are right, Guilaume. If this code is the bottleneck, the vectorized code should be preferred. And I tend to move bottlenecks even to C-MEX code, but in this case this would be efficient only, if only one specific operation should be performed with the data.
I get a different timing, when I move your code into a function with one command per line, such that the JIT can perform the improvements:
Elapsed time is 7.257640 seconds.
Elapsed time is 5.866015 seconds.
(Matlab 2011b, Core2Duo 2.3GHz, Win7/64)
The core of my argument remains: When speed matters here, it might be better to re-organize the data representation:
function test
budget = rand(1, 1e5);
S1 = struct('budget', num2cell(budget), 'otherfield', 5);
tic;
subresult = num2cell([S1.budget] - 5);
[S1.budget] = subresult{:};
toc
S2.budget = budget;
S2.otherfield = 5;
tic;
S2.budget = S2.budget - 5;
toc
% Elapsed time is 0.192276 seconds.
% Elapsed time is 0.001099 seconds.
This is a speedup of factor 175. Therefore I'd prefer to keep the values in an array, when they are subject to mathematical operations. But there might be other parts of the code, where the splitting into fields of a struct is an important benefit.
Alex
Alex 2015년 3월 16일
It is not clear to me why you do this. The code here does not subtract from multiple structures at once.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by