So I'm working on this code to help me with a class, and it works except for the fact that it doesn't store all of my x answers into the array, it only stores the last one. Any suggestions would be so helpful, oh I'm using the R2012a on a macbook pro (don't really know if you need this info)
n = 1;
x = 0;
for n = 1:365
w = 45;
B = (n-1)*(360/365);
d = (180/pi)*(0.006918-00.399912*cos(B)+0.070257*sin(B)-0.006758*cos(2*B)+0.000907*sin(2*B)-0.002697*cos(3*B)+0.00148*sin(3*B));
c = x;
x = sin(d)* sin(o)*cos(t) - sin(d)*cos(o)*sin(t)*cos(s) + cos(d)*cos(o)*cos(t)*cos(w) + cos(d)*sin(o)*sin(t)*cos(s)*cos(w) + cos(d)*sin(t)*sin(s)*sin(w);
%plot (d,x);
A = [45 x]
end
n = n+1;

 채택된 답변

Jos (10584)
Jos (10584) 2015년 2월 24일
편집: Jos (10584) 2015년 2월 25일

0 개 추천

You want to do something like this:
N = 10 ;
A = zeros(N,1) ; % pre-allocation, will speeds things up
for k=1:N,
x = rand(1) + 3 ; % some calculation
A(k) = x ; % store it
end
However, note that in matlab you can often vectorise loops. All of the lines above vectorises into:
A = rand(10,1)+3 ;

댓글 수: 2

Leah Aguirre
Leah Aguirre 2015년 2월 24일
Thank you! It works perfectly
Stephen23
Stephen23 2015년 2월 25일
@Leah Aguirre: the vectorized solution is the one you should use! And you should learn about how to write your own vectorized code too.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

질문:

2015년 2월 24일

댓글:

2015년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by