필터 지우기
필터 지우기

Removal of For Loops

조회 수: 1 (최근 30일)
Ben
Ben 2012년 2월 1일
Is it possible to script the following function in MATLAB without a for loop, or any other iterative loop? I am trying to understand if it is possible to have a matrix reference itself as it is populated using a single command in MATLAB. Thanks for looking!
x(1)=0;
x(2)=1;
x(3)=2;
for n=4:51
x(n)=x(n-1) + x(n-3);
end

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 2월 1일
It's probably possible with filter() or similar. But I guarantee a well written for-loop will not be much slower. Just remember to preallocate x.
x = zeros(51,1);
%etc.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 1일
I = sqrt(-1);
x = @(n) -(301/14945472)*((1+I*3^(1/2))*(93^(1/2)-93/7)*(108+12*93^(1/2))^(1/3)-744/7-(5/14)*(-1+I*3^(1/2))*(93^(1/2)-31/5)*(108+12*93^(1/2))^(2/3))*(((1/72)*(-93^(1/2)+9)*(108+12*93^(1/2))^(2/3)+(1/6)*(108+12*93^(1/2))^(1/3))^n*(((I*3^(1/2)-67/43)*93^(1/2)+279/43-((775/43)*I)*3^(1/2))*(108+12*93^(1/2))^(1/3)+((-((97/129)*I)*3^(1/2)+27/43)*93^(1/2)+((248/43)*I)*3^(1/2)-310/43)*(108+12*93^(1/2))^(2/3)+1860/43+((92/43)*I)*3^(1/2)*93^(1/2))*((1/72)*(-9+93^(1/2))*(-1+I*3^(1/2))*(108+12*93^(1/2))^(1/3)-(1/72)*(108+12*93^(1/2))^(2/3)-((1/72)*I)*(108+12*93^(1/2))^(2/3)*3^(1/2)+1/3)^n+(6696/43)*(-(1/72)*(-9+93^(1/2))*(1+I*3^(1/2))*(108+12*93^(1/2))^(1/3)-(1/72)*(108+12*93^(1/2))^(2/3)+((1/72)*I)*(108+12*93^(1/2))^(2/3)*3^(1/2)+1/3)^n*((1/72)*(-93^(1/2)+9)*(108+12*93^(1/2))^(2/3)+(1/6)*(108+12*93^(1/2))^(1/3))^n+((-((12/43)*I)*3^(1/2)+98/43)*93^(1/2)-1302/43-((248/43)*I)*3^(1/2))*(108+12*93^(1/2))^(1/3)+((-((89/129)*I)*3^(1/2)+35/43)*93^(1/2)+((279/43)*I)*3^(1/2)-217/43)*(108+12*93^(1/2))^(2/3)+1860/43-((92/43)*I)*3^(1/2)*93^(1/2))/((1/72)*(-93^(1/2)+9)*(108+12*93^(1/2))^(2/3)+(1/6)*(108+12*93^(1/2))^(1/3))^n
But watch out for floating point round-off.
(Yes, really. And yes, this is the simplified form of the expression.)
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2012년 2월 1일
nice! +1 for sure.
Walter Roberson
Walter Roberson 2012년 2월 1일
Note: the above was produced by simplifying the output of Maple's rsolve() routine. The basic form of the answer is not very complicated, but it involves the sum of terms with the sum taken over the roots of a cubic expression, and that cubic happens to have two imaginary solutions. The expanded expression before simplification is pretty grotty.

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by