• Register
0 votes
1.2k views

Problem :

I am facing following error message:

Index in position 1 is invalid. Array indices must be positive integers or logical values.

My code is as follows:

clc;

close all;

clear;

PD=phantom('Modified Shepp-Logan');

padimage = [2,2];

PD= padarray(PD,padimage);

subplot(2,3,1)

imagesc(PD);

colormap('gray');

title('Circle Phantom')

xlabel('X')

ylabel('Y')

freq = 2;

thetas = 0:freq:180;

gtheta  = length(thetas);

gl = size(PD,1);

sinogram = zeros(gl,gtheta);

for i = 1:length(thetas)

   tmpImage      = imrotate(PD,-thetas(i),'bilinear','crop');

   sinogram(:,i) = sum(tmpImage);

end

subplot(2,3,2)

imagesc(sinogram);

title('Circle Sinogram');

xlabel('l');

ylabel('\theta');

thetas=0;

Fl = size(sinogram,1);

Ftheta  = length(thetas);

thetas = (pi/180)*thetas;

g0 = zeros(Fl,Fl);

Fmid = ceil(Fl/2);

 [x,y] = meshgrid(ceil(-Fl/2):ceil(Fl/2-1));

for i = 1:Ftheta

   rotCoords = Fmid+round(x*sin(thetas(i)) + y*cos(thetas(i)));

    rotCoords=floor(abs(rotCoords));

   g0 = g0 + sinogram(rotCoords,i)./Ftheta;

end

subplot(2,3,3);

imagesc(g0);

title('Simple backprojection')

xlabel('X')

ylabel('Y')

The error message is occurring as follow:

Index in position 1 is invalid. Array indices must be positive integers or logical values.

Can anybody tell me why the issue is here?

7.5k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

You are trying as follows:

Fmid = ceil(Fl/2);

[x,y] = meshgrid(ceil(-Fl/2):ceil(Fl/2-1));

rotCoords = Fmid+round(x*sin(thetas(i)) + y*cos(thetas(i)));

Generally when sin(thetas) is equal to 1 then cos(thetas) is equal to 0. So the rotCoords would be Fmid + round(x*1 + y*0) which can be as low as ceil(Fl/2) + ceil(-Fl/2) which will be 0. So the rotCoords can comes out 0, which is clearly the invalid index.

Please Note : In practice your rotCoords are in the range 0 to 259, where 259 is Fl-1

38.6k points
0 votes

Solution:

1. somehow you have aa variable in your workspace that is named Px so matlab thinks you are indexing instead of calling a function.

  1. we think we just solved it by renaming Px to some other name. What do you mean by a workspace here, because on my main program, we don't see any Px only except for the function call
  2. Your code is not a function, so if you were to do
  3. Px = 'hello';
    1. before running the code then Px would be considered to be a variable instead of a function. You might have assigned something to Px in an earlier version of the code and not cleared your variables since then. Using functions instead of scripts protects against this kind of accidental use of left-over variables. Also you are using load() without assigning the output to a variable. If you happened to have a variable named Px in the .mat file, the stored value would become active. It is recommended that you assign load() to a variable and access the structure, something like

Follow the following Code:

filestruct = load('lowpasssignal.mat');
dsp = filestruct.dsp;   %pull out the specific variable you need from the structure.

 

31.7k points

Related questions

0 votes
1 answer 58 views
58 views
Problem: This code seems like it should be simple, but for some reason I keep getting this error: "Index in position 2 is invalid. Array indices must be positive integers or logical values." It refers to these lines: Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2))); VR(1,i) = Vr; I've tried checking if I ... = 0:1000 w=i*10*10^-9; Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2))); VR(1,i) = Vr; end Thanks!
asked Apr 25 muktaa 34.6k points
0 votes
1 answer 68 views
68 views
Problem: This code seems like it should be simple, but for some reason I keep getting this error: "Index in position 2 is invalid. Array indices must be positive integers or logical values." It refers to these lines: Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2))); VR(1,i) = Vr; I've tried checking if I ... = 0:1000 w=i*10*10^-9; Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2))); VR(1,i) = Vr; end Thanks!
asked Apr 25 muktaa 34.6k points
0 votes
1 answer 81 views
81 views
Problem: Please Support is needed >An array indices must be positive integers or have logical values.
asked May 9 Isac Christiaan 103k points
0 votes
1 answer 45 views
45 views
Problem: How do I do it ? >Matlab array indices must be positive integers or logical values
asked May 4 ochena 23.3k points
0 votes
1 answer 25 views
25 views
Problem: What am i doing wrong? When I type, [f]= NC(-3,0,fun) and set fun= @(x)normpdf(x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
asked May 3 salim1 60.6k points
0 votes
1 answer 62 views
62 views
Problem: When I type, [f]= NC(-3,0,fun) and set fun= @(x)normpdf(x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
asked May 3 sumaiya simi 43.9k points
0 votes
1 answer 42 views
42 views
Problem: What am I doing wrong? When I type, [f]= NC(-3,0,fun) and set fun= @(x)normpdf(x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
asked May 2 ochena 23.3k points
0 votes
1 answer 234 views
234 views
Problem: i don’t understand this problem? please help me. matlab array indices must be positive integers or logical values.
asked Apr 16 Rohit kr 17.5k points
0 votes
1 answer 12 views
12 views
Problem: What am I doing wrong? When I type, [f]= NC(-3,0,fun) and set fun= @(x)normpdf(x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
asked May 27 Junia Phoebe 83.8k points