Nested loop error help

1 view (last 30 days)
Amir Alansari
Amir Alansari on 15 Mar 2014
Edited: Azzi Abdelmalek on 15 Mar 2014
Hello!
I guess this should be super obvious and simple for almost everyone...but for some reason I can't make this nested for loop to work:
I get this error and I'm not sure why: Attempted to access b(1,7); index must be a positive integer or logical.
b = zeros(51,11);
for i = 0:0.01:0.5
for j = 0:0.1:1
a = (2*i)+j;
p = (j/0.1)+1;
q = (i/0.01)+1;
b(q,p)= a;
end
end
Thanks!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 15 Mar 2014
Edited: Azzi Abdelmalek on 15 Mar 2014
EDIT
b = zeros(51,11);
for i = 0:0.01:0.5
for j = 0:0.1:1
a = (2*i)+j;
p = round((j/0.1)+1);
q = round((i/0.01)+1);
b(q,p)= a;
end
end
  3 Comments
Amir Alansari
Amir Alansari on 15 Mar 2014
Edited: Amir Alansari on 15 Mar 2014
Thanks a lot that solved my problem!
I still couldn't figure out why I was having the problem, based on the "Walter link" below..but int32 seems to work fine for me.
Azzi Abdelmalek
Azzi Abdelmalek on 15 Mar 2014
Using fix is a bad idea, you can replace it with round

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 15 Mar 2014

Image Analyst
Image Analyst on 15 Mar 2014
Try this:
b = zeros(51,11);
for i = 0:0.01:0.5
for j = 0:0.1:1
a = (2*i)+j;
p = int32(10*j+1);
q = int32(100*i+1);
b(q,p)= a;
end
end

Community Treasure Hunt

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

Start Hunting!