Why this error is coming in my code?

zeros(1500,2);
for j=1:3
zeros(j,1)=randi([1,5],1,1);
zeros(j,2)=randi([1,5],1,1);
end
ones(1500,2);k=0;
if true
% code
end
while k<365
ones(k,1) = 1*2*zeros(k,1);% i have assumed velocity in x direction to be 2m/d.
ones(k,2) = 1*1*zeros(k,2);% velocity in y direction is assumed to be 1m/d.
end
Subscript indices must either be real positive integers or logicals.
Error in oncemore (line 8) ones(k,1) = 1*2*zeros(k,1);% i have assumed velocity in x direction to be 2m/d.

Answers (1)

There are many problems in your code The first line:
zeros(1500,2);
What this line means? then after you wrote
zeros(j,1)=randi([1,5],1,1)
Which will destroy the built-in function zeros

3 Comments

anchal garg
anchal garg on 26 Jun 2015
Edited: anchal garg on 26 Jun 2015
Zeros(1500,2) means an array of 1500x2.ya i know but actually i want to have array in Matlab that will store these random numbers but i don't think there is a way to make an array in matlab so i have to use zeros ,though it's not working also.
m=4
n=2
a=randi(5,m,n)
Guillaume
Guillaume on 26 Jun 2015
Edited: Guillaume on 26 Jun 2015
@anchal, assuming it's the first run of your script, the line
zeros(1500,2);
means Create a matrix of zeros of size 1500x2 and because I don't store it anywhere, immediately forget about it. Basically, it does nothing but waste time.
You then go on to create a variable call zeros which will prevent the built-in matlab zeros function from working.
You then do the exact same thing with ones. Invoke the function, but do nothing with the output and then prevent the function from working again (until you clear your variables)
I would recommend that you go through the getting started tutorials in the documentation.

This question is closed.

Asked:

on 26 Jun 2015

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!