필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How can I preallocate me code?

조회 수: 1 (최근 30일)
Reni Llupa
Reni Llupa 2017년 11월 27일
마감: MATLAB Answer Bot 2021년 8월 20일
How can I preallocate my code. Creating x_o=zeros(1,10000) does not help and makes it slower.I will attach the code above. Thanks in advance.
  댓글 수: 1
Rik
Rik 2017년 11월 27일
You need to rethink your approach. You can generate the dice rolls in one vector, but the recursive nature of your method of calculating the coordinates will lead to very slow code. Apart from re-writing your code or lowering the loop count, there is not really a solution I can see.
And you are not using indices, so that pre-allocation does nothing apart from slowing down your code even more.
x_0(i)=(x_o(i-1)+x_a)/2;%don't forget the case where i==1

답변 (1개)

KL
KL 2017년 11월 27일
편집: KL 2017년 11월 27일
Use indexing. Pre-allocating won't make a big difference if you have a for loop there.
roll = randi([1 6], 1, 10000);
indx_a = roll<=2;
indx_b = roll>2 & roll<=4;
x_o = ones(1,10000).*x_c/2;
y_o = ones(1,10000).*y_c/2;
x_o(indx_a+indx_b) = indx_a.*(x_a/2)+indx_b.*(x_b/2);
y_o(indx_a+indx_b) = indx_a.*(y_a/2)+indx_b.*(y_b/2);

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by