How can I add the values of each iteration of a for loop, where the index is k=0:0.001:.30, into a matrix so that I can plot the values?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a 'for' loop that is calculating an eqn from k=0:0.001:0.30, the loop is generating the required results for each single iteration but when I try to take each iteration and put it into a matrix I get the following error,
Subscript indices must either be real positive integers or logicals.
I think this is because of my indexing k=0:0.001:0.30, MatLab doesn't like the zeros, but I need to run the loop in this fashion to get the results I need.
This is what I have for the 'for' loop,
% preallocate space x= zeros(300,1);
for k = 0:0.001:0.30
y=3.065-(8.871*(k/H))+(14.036*(k/H).^2)-(7.219*(k/H).^3);
x(:,k)=y % store y as kth column of x
end
채택된 답변
Youssef Khmou
2014년 5월 20일
편집: Youssef Khmou
2014년 5월 20일
If H is scalar you can vectorize the problem :
k=0:0.001:0.30;
H=2;
y=3.065-(8.871*(k/H))+(14.036*(k/H).^2)-(7.219*(k/H).^3);
Using the loop, the index must be an integer, to respect this condition you can proceed as :
k=0:0.001:0.30;
for t=1:length(k)
y=3.065-(8.871*(k(t)/H))+(14.036*(k(t)/H).^2)-(7.219*(k(t)/H).^3);
x(:,t)=y; % store y as kth column of x
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!