필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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?

채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 13일
cash = struct('PaidOut', zeros(n,500), 'PaidIn', zero(n,500), 'TaxesPaid', zeros(n,500));
See also structfun()

추가 답변 (1개)

Shaun VanWeelden
Shaun VanWeelden 2013년 3월 13일
You can assign the same value to multiple fields if you want, which I think would help in this case.
  댓글 수: 3
Walter Roberson
Walter Roberson 2013년 3월 13일
Beware, though, the copy-on-write behavior.
Michael
Michael 2013년 3월 13일
Could you give an example of the syntax? a=b=25 doesn't seem to work.

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

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by