0

I am very new in Matlab. I made a for loop with i to m ,and j to n. I wrote this code to take a submatrix of a matrix but it keeps giving me this error

Subscript indices must either be real positive integers or logicals. this is the code

for i=1:m,
for j = 1:n,
    display(i);
    display(j);

            edgeil = 2;
    edgeib = 2; 
    edgejb = 2; 
    edgejl = 2;


    if((i-CenteriSE)< 0)
       edgeib = CenteriSE - (-1)*(i-CenteriSE);

    end
    if((i+ CenteriSE)> m)
        temp = i+ CenteriSE - m;
        edgeil = CenteriSE - temp;
    end

    if((j-CenterjSE)< 0)
        edgejb = CenterjSE- (-1)*(j-CenterjSE);

    end
    if((j+ CenterjSE)> n)
        temp2 = j+ CenterjSE - n;
        edgejl = CenterjSE - temp2;
    end

    bok1 = round(edgeib);
    bok2 = round(edgeil);
    bok3 = round(edgejb);
    bok4 = round(edgejl);
    display(bok1);
    display(bok2);
    if( (bok1 == round(bok1)) && (bok2 == round(bok2)) && (bok3 == round(bok3)) && (bok4 == round(bok4)))
    B = circles(i-round(bok1):i+round(bok2),j-round(bok3):j+round(bok4));
    end

I wrote that if statement and round s to correct it but it doesnt work. Please help me how can I fix this?

Ada
  • 624
  • 3
  • 17
  • 31
  • CenteriSE and CenterjSE are both 2. It doesn't get out of range. – Ada Mar 05 '11 at 21:33
  • I also used abs(..) but I don't know why id gives me that error all the time. A little help please? – Ada Mar 05 '11 at 21:39
  • Does it say what line? Have you tried `circles(round(i-bok1):round(i+bok2), round(j-bok3):round(j+bok4));`? (There might be an off-by-one there...) – rlibby Mar 05 '11 at 22:01
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 15:47

1 Answers1

0

well, it's simple. first let's just remove all the clutter. you say CenteriSE=2, so this statement

edgeib = CenteriSE - (-1)*(i-CenteriSE); is equivalent to edgeib=i for i=1.

Now if you go to your last statement, B = circles(i-round(bok1):i+round(bok2),j-round(bok3):j+round(bok4));, you're doing i-round(bok1), which is just i-i=0 when i=1. Matlab's indexing start from 1, and this is why you get this error.

  • yes thank you I realized it after I checked my equation. I don't have a very nice math :) – Ada Mar 06 '11 at 15:38