• Register
0 votes

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:


close all;


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

padimage = [2,2];

PD= padarray(PD,padimage);




title('Circle Phantom')



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);




title('Circle Sinogram');




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)));


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




title('Simple backprojection')



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?

6 5 3
7,540 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

9 7 4
38,600 points
0 votes


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.


10 6 4
31,120 points

Related questions

0 votes
1 answer 93 views
Problem : The below error occurs quite frequently to me: “Subscript indices must either be real positive integers or logical” I want to have a general solution for dealing with above problem.
asked Jan 28, 2020 jwilliam 3.9k points
0 votes
2 answers 67 views
Problem : I am currently trying to run my code but found the really annoying problem which I am unable to solve. The function that I am currently working out is as below; function myout = MyCointPairs(MyPriceMat, MySymbols) myout=[]; MyNofStocks = size(MyPriceMat, 2); ... is the price matrix of 60x10, and "MySymbols" is the 10x1 string. One more doubt from my side what are "Subscript indices"?
asked Dec 25, 2019 alecxe 7.5k points
0 votes
1 answer 17 views
Problem: Hello! I am completely new to Python and trying to grasp concepts in Python. While I was learning lists and dictionaries, I wrote the following program: name = ["John", "Alice", "Catherine"] diction = {"name" : "Alice"} print(name[diction]) When I ... ; as a value and the program should print “Alice” on screen. I do not know what I have done wrong. Can anybody help?
asked Dec 18, 2020 Code Learner 5.5k points
0 votes
1 answer 22 views
1 vote
1 answer 67 views
Problem : I am very new to Programming. Currently I am trying to learn the Python. I am trying to slice my list (myplateau) in the several list (L[i]) but because of some unknown reasons to me I am facing the below error message: File "C:\Users\Martin\Skycraper ... ]     L.append(myitem)     i += 1 I am very confused about above error. Please help me in fixing above mentioned python related error.
asked May 13, 2020 Martin K 6.6k points
1 vote
2 answers 47 views
Problem: I wrote a program: def only(): names = ['Laia', 'Poter', 'Harrold', 'Annabell', 'Preety', 'Whina', 'Cory', 'Phase'] put = input('Search with name:') binary_search(names, put) if position == -1: print("The name entered is not part ... in binary_search if names[middle] == entered: TypeError: list indices must be integers or slices, not float Why is this error happenning can anyone explain?
asked Apr 3, 2020 LizzyM 6.1k points
3 votes
2 answers 233 views
Problem: I am trying to learn python3 and tuple. It was going pretty smooth unless I tried to access an element from a tuple of my program. It throws back an error: typeerror: tuple indices must be integers or slices, not tuple What's wrong? Any explanation?
asked Mar 25, 2020 Gavin 15.3k points
1 vote
3 answers 622 views
Problem : I'm a newbie with Python's arrays and I don't understand how I can avoid below error: python typeerror: list indices must be integers or slices, not str
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 19 views
Problem: Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python? list.index(x[, start[, end]]) Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.
asked Jan 10 Mashhoodch 6.1k points