Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Subscript indices must either be real positive integers or logicals Error + Code

Subject: Subscript indices must either be real positive integers or logicals Error + Code

From: Jerome

Date: 20 Feb, 2013 17:24:10

Message: 1 of 3

was wondering if someone can explain to me what this error means. And, if possible, how to resolve it? ??? Subscript indices must either be real positive integers or logicals.

Error in ==> interp2>linear at 344
F = ( arg3(ndx).*(onemt) + arg3(ndx+1).*t ).*(1-s) + ...

Error in ==> interp2 at 220
zi = linear(ExtrapVal,x,y,z,xi,yi);

Error in ==> snake at 71
ssx = gamma*xs - kappa*interp2(fx,xs,ys);

This is my complete code:

image = imread('image.jpg');

%parameters
alpha = 0.001;
beta = 0.4;
kappa=0.0001;
gamma = 100;
N = 100;
wl = 10;
we = 10;
wt = 10;
smth = rgb2gray(image);

% Calculating size of image
[row col] = size(image)

eline = smth; %eline is simply the image intensities

[grady,gradx] = gradient(double(smth));

eedge = -1 * sqrt ((gradx .* gradx + grady .* grady)); %eedge is measured by gradient in the image

m1 = [-1 1];
m2 = [-1;1];
m3 = [1 -2 1];
m4 = [1;-2;1];
m5 = [1 -1;-1 1];

  cx = conv2(smth,m1,'same');
  cy = conv2(smth,m2,'same');
  cxx = conv2(smth,m3,'same');
  cyy = conv2(smth,m4,'same');
  cxy = conv2(smth,m5,'same');

  for i = 1:700
    for j= 1:900
    % eterm as deined in Kass et al Snakes paper
    eterm(i,j) = (cyy(i,j)*cx(i,j)*cx(i,j) -2 *cxy(i,j)*cx(i,j)*cy(i,j) + cxx(i,j)*cy(i,j)*cy(i,j))/((1+cx(i,j)*cx(i,j) + cy(i,j)*cy(i,j))^1.5);
end
end

eext = (double(wl.*eline) + double(we.*eedge -wt) .* eterm); %eext as a weighted sum of eline, eedge and eterm

[fx, fy] = gradient(eext); %computing the gradient

xs=1:900;
xs=xs';
ys=repmat(242,1,900);
ys=ys'

[m n] = size(xs);
[mm nn] = size(fx);

%populating the penta diagonal matrix
A = zeros(m,m);
b = [(2*alpha + 6 *beta) -(alpha + 4*beta) beta];
brow = zeros(1,m);
brow(1,1:3) = brow(1,1:3) + b;
brow(1,m-1:m) = brow(1,m-1:m) + [beta -(alpha + 4*beta)]; % populating a template row
 for i=1:m
   A(i,:) = brow;
    brow = circshift(brow',1)'; % Template row being rotated to egenrate different rows in pentadiagonal matrix
 end

 [L U] = lu(A + gamma .* eye(m,m));
 Ainv = inv(U) * inv(L); % Computing Ainv using LU factorization

 %moving the snake in each iteration
for i=1:N;

ssx = gamma * xs - kappa * interp2(fx,xs,ys); %This is where I receive the error
ssy = gamma * ys - kappa * interp2(fy,xs,ys);

%calculating the new position of snake
xs = Ainv * ssx;
ys = Ainv * ssy;


%Displaying the snake in its new position
 % imshow(image,[]);
 % hold on;
       plot([xs; xs(1)], [ys; ys(1)], 'r-');
 % hold off;
 % pause(0.001)
        end;

Subject: Subscript indices must either be real positive integers or logicals

From: dpb

Date: 20 Feb, 2013 22:56:30

Message: 2 of 3

On 2/20/2013 11:24 AM, Jerome wrote:
> was wondering if someone can explain to me what this error means. And,
> if possible, how to resolve it? ??? Subscript indices must either be
> real positive integers or logicals.

Pretty much exactly what it says--Matlab arrays are 1-based so all
indices have to start at (at least) one ("real positive integers" part)
or be logical indices which are an array of T,F that select the subset
within the array on that logic basis rather than by absolute position.

> Error in ==> interp2>linear at 344
> F = ( arg3(ndx).*(onemt) + arg3(ndx+1).*t ).*(1-s) + ...
>
> Error in ==> interp2 at 220
> zi = linear(ExtrapVal,x,y,z,xi,yi);
>
> Error in ==> snake at 71
> ssx = gamma*xs - kappa*interp2(fx,xs,ys);
>
> This is my complete code:
>
...

> eext = (double(wl.*eline) + double(we.*eedge -wt) .* eterm); %eext as a
> weighted sum of eline, eedge and eterm
>
> [fx, fy] = gradient(eext); %computing the gradient
...

Somehow you've passed an illegal set of inputs to interp2() that has
caused it to error. You can tell this by tracing back the errors to the
initiating call. I don't have time at present to try to delve deeply
into your code but the three-argument form of the call to interp2
assumes the V (fx) here is a set of function values that match 1:M and
1:N for the size of the array in the two dimension. I'm suspecting this
probably isn't so is the root cause that confused interp2.

Study

doc interp2

and the examples carefully to see where it went wrong.

--

Subject: Subscript indices must either be real positive integers or logicals

From: dpb

Date: 21 Feb, 2013 14:42:42

Message: 3 of 3

On 2/20/2013 4:56 PM, dpb wrote:
> On 2/20/2013 11:24 AM, Jerome wrote:
>> was wondering if someone can explain to me what this error means. And,
>> if possible, how to resolve it? ??? Subscript indices must either be
>> real positive integers or logicals.
>
> Pretty much exactly what it says--Matlab arrays are 1-based so all
> indices have to start at (at least) one ("real positive integers" part)
> or be logical indices which are an array of T,F that select the subset
> within the array on that logic basis rather than by absolute position.
>
>> Error in ==> interp2>linear at 344
>> F = ( arg3(ndx).*(onemt) + arg3(ndx+1).*t ).*(1-s) + ...
>>
>> Error in ==> interp2 at 220
>> zi = linear(ExtrapVal,x,y,z,xi,yi);
>>
>> Error in ==> snake at 71
>> ssx = gamma*xs - kappa*interp2(fx,xs,ys);
>>
>> This is my complete code:
>>
> ...
>
>> eext = (double(wl.*eline) + double(we.*eedge -wt) .* eterm); %eext as a
>> weighted sum of eline, eedge and eterm
>>
>> [fx, fy] = gradient(eext); %computing the gradient
> ...
>
> Somehow you've passed an illegal set of inputs to interp2() that has
> caused it to error. You can tell this by tracing back the errors to the
> initiating call. I don't have time at present to try to delve deeply
> into your code but the three-argument form of the call to interp2
> assumes the V (fx) here is a set of function values that match 1:M and
> 1:N for the size of the array in the two dimension. I'm suspecting this
> probably isn't so is the root cause that confused interp2.
...

Oh, in passing I'll note another way one can cause the above message is
by accidentally aliasing a function w/ an array in the workspace--for
example if you had inadvertently typed an

interp2=fx,xs,ys);

at the command line and then recalled and cleared the RHS and
accidentally hit CR interp2 could be an empty array. Then a call to
interp2 w/ real arguments would cause the problem. This isn't the case
here as the error shows the line in the actual function; I just point it
out as a fairly common error...

WRT to your actual problem, one guesses that somehow the ndx values in
the expression aren't integers, maybe as a clue. You can use debug w/
break on error and inspect the expression to see what actually is
getting passed to get some idea where things went wrong that may help in
correcting the call.

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us