user2861089
user2861089

Reputation: 1235

Store data from for loop in matrix - keep getting error

Sorry, I know this question has been asked a lot and I have looked at those answers but I still can't solve my problem and I'm getting the following error:

Subscript indices must either be real positive integers or logicals.

Here is my code:

for i = 0:10, 

    x1 = rand(1);
    x2 = rand(1);
    x = [x1 x2];

Y(i,:) = [x(1) x(2)]
end

Upvotes: 0

Views: 25

Answers (1)

David
David

Reputation: 8459

Matlab indexing starts at 1, not 0 as in some other languages. So in the first iteration of your loop, you have i=0, but you can't do y(0,:). With the code you have posted, just do for i=1:11 to get 11 iterations. (Alternatively, you could just do rand(11,2) if the only goal of the code is to generate the matrix.)

Upvotes: 3

Related Questions