MATLAB Answers

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

8 views (last 30 days)
Michael Tross
Michael Tross on 30 Oct 2019
Answered: Alex Mcaulley on 30 Oct 2019
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;

  0 Comments

Sign in to comment.

Answers (1)

Alex Mcaulley
Alex Mcaulley on 30 Oct 2019
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

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags