Is there a good way to avoid junking up code with lots of preallocation lines?

I have a specific set of code that has a lot of little projection pieces, each of which I'd like to be preallocated for speed. However, in doing so, you get a lot of lines like this:
...
cash.PaidOut=zeros(n,500);
cash.PaidIn=zeros(n,500);
cash.TaxesPaid=zeros(n,500);
...
Is there a way to get the same functionality in a more terse style?

 Akzeptierte Antwort

cash = struct('PaidOut', zeros(n,500), 'PaidIn', zero(n,500), 'TaxesPaid', zeros(n,500));
See also structfun()

Weitere Antworten (1)

Shaun VanWeelden
Shaun VanWeelden am 13 Mär. 2013
You can assign the same value to multiple fields if you want, which I think would help in this case.

3 Kommentare

Multiple fields at one time/line if that was not clear before.
Beware, though, the copy-on-write behavior.
Could you give an example of the syntax? a=b=25 doesn't seem to work.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by