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!

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 15 de Mzo. de 2014
Editada: Azzi Abdelmalek el 15 de Mzo. de 2014

0 votos

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 comentarios

Image Analyst
Image Analyst el 15 de Mzo. de 2014
Amir, be aware, fix() truncates the fractional part so that if you're just slightly under the integer number, you'll go down one whole integer. that's why I used int32(), which is a rounding like you're familiar with followed by a cast to an integer class. See this demo code:
p=[1.01, 2.99999]
fp = fix(p)
rp = int32(p)
p = 1.01 2.99999
fp = 1 2
rp = 1 3
That's why I suggested int32() instead of fix() in my naswer.
Amir Alansari
Amir Alansari el 15 de Mzo. de 2014
Editada: Amir Alansari el 15 de Mzo. de 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 el 15 de Mzo. de 2014
Using fix is a bad idea, you can replace it with round

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 15 de Mzo. de 2014

0 votos

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

Categorías

Preguntada:

el 15 de Mzo. de 2014

Editada:

el 15 de Mzo. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by