Effacer les filtres
Effacer les filtres

Subtract a constant value from some elements in a struct

7 vues (au cours des 30 derniers jours)
Alex
Alex le 14 Mar 2015
Commenté : Alex le 16 Mar 2015
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?

Réponses (3)

Image Analyst
Image Analyst le 14 Mar 2015
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 le 14 Mar 2015
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 le 14 Mar 2015
Modifié(e) : Jan le 14 Mar 2015
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 commentaires
Jan
Jan le 14 Mar 2015
Modifié(e) : Jan le 14 Mar 2015
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 le 16 Mar 2015
It is not clear to me why you do this. The code here does not subtract from multiple structures at once.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by