how do I append to an array within a loop with float values?

lenArray = length(0:0.1:20);
solutions = zeros(lenArray,1);
for k=0:0.1:20
a = -k;
b = -k;
soln= a+b;
solutions(k+1)= soln;
end
Array indices must be positive integers or logical values.
Error in EXAMPLE (line 7)
solutions(k+1)= soln;

답변 (1개)

Alex Mcaulley
Alex Mcaulley 2019년 10월 30일
Try with this:
k = 0:0.1:20;
solutions = zeros(size(k));
for ii = 1:numel(k)
a = -k(ii);
b = -k(ii);
soln= a+b;
solutions(ii)= soln;
end
or without loop:
k = 0:0.1:20;
solutions = -2*k

카테고리

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

태그

질문:

2019년 10월 30일

답변:

2019년 10월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by