Index in position 1 is invalid. Array indices must be positive integers or logical values?
189 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Josh Allen
am 27 Sep. 2018
Kommentiert: Walter Roberson
am 23 Mär. 2023
I am getting the following error message: Index in position 1 is invalid. Array indices must be positive integers or logical values.
My code is:
clc;
close all;
clear;
% The radon transform of Circle Phantom
PD=phantom('Modified Shepp-Logan');
% pad the image with zeros
padimage = [2,2];
PD= padarray(PD,padimage);
% Original image
subplot(2,3,1)
imagesc(PD);
colormap('gray');
title('Circle Phantom')
xlabel('X')
ylabel('Y')
% Theta is 0:180 with the increment of 0.5 degrees
freq = 2;
thetas = 0:freq:180;
% compute sinogram / radon transformation
gtheta = length(thetas);
gl = size(PD,1);
sinogram = zeros(gl,gtheta);
% loop for the number of angles
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');
% The inverse radon transform with only 1 back projection that is theta = 0
thetas=0;
Fl = size(sinogram,1);
Ftheta = length(thetas);
% convert thetas to radians
thetas = (pi/180)*thetas;
% set up the backprojected image
g0 = zeros(Fl,Fl);
% find the middle index of the projections
Fmid = ceil(Fl/2);
% set up the coords of the image
[x,y] = meshgrid(ceil(-Fl/2):ceil(Fl/2-1));
% loop over each projection
for i = 1:Ftheta
% Using the back projection formula
rotCoords = Fmid+round(x*sin(thetas(i)) + y*cos(thetas(i)));
% % check which coords are in bounds
% indices = find((rotCoords > 0) & (rotCoords <= Fl));
% newCoords = rotCoords(indices);
% summation
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 for line 54:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in CT_HW3_BP (line 54)
g0 = g0 + sinogram(rotCoords,i)./Ftheta;
Can anyone tell me what the issue is here?
3 Kommentare
hema
am 23 Mär. 2023
Index in position 1 is invalid. Array indices must be positive integers or logical values.
A([i,j],:)=A([j,i],:);
my code is:
for i=0:20
for j=0:20
if i==j
A=zeros(i,j);
else
A([i,j],:)=A([j,i],:);
end
please help me
Walter Roberson
am 23 Mär. 2023
0 is not a valid index
Note that A=zeros(i,j); overwrites all of the array A
Akzeptierte Antwort
Walter Roberson
am 27 Sep. 2018
You have
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)));
when sin(thetas) is 1 then cos(thetas) is 0. so rotCoords would be Fmid + round(x*1 + y*0) which could be as low as ceil(Fl/2) + ceil(-Fl/2) which would be 0. Therefore rotCoords can come out 0, which is not a valid index.
In practice your rotCoords are in the range 0 to 259, where 259 is Fl-1
0 Kommentare
Weitere Antworten (2)
Kazi Shakib
am 24 Okt. 2018
Bearbeitet: Walter Roberson
am 24 Okt. 2018
V=imresize(im,[256,256]);
[m1,n1] = size(V);
blocksize=2;
nob=m1*n1/(blocksize*blocksize);
kk1=0;
for i1=1:(m1/blocksize)
for j1=1:(n1/blocksize)
J=V((blocksize*(1i-1)+1:blocksize*(1i-1)+blocksize),(blocksize*(1j-1)+1:blocksize*(1j-1)+blocksize));
kk1=kk1+(m1/blocksize);
end
end
I have the same probz here..what to do??
1 Kommentar
Walter Roberson
am 24 Okt. 2018
1i is an imaginary number, so you are trying to index at an imaginary number.
William Harris
am 5 Feb. 2021
Hi, I'm getting the same error when I use roi_5, the contents of which are shown below, to index into an image. I'm assuming that the issue is a result of the '10e-09*' at the start. Does anyone know how to deal with this? Or is something else likely to be causing the error?
roi_5 is defined as;
roi_5 = R1rho(22:30, 28:36);
The contents of roi_5:
roi_5 =
1.0e-09 *
0.0313 0.0342 0.0347 0.0307 0.0337 0.0318 0.0355 0.0381 0.0394
0.0329 0.0345 0.0411 0.0412 0.0458 0.0432 0.0456 0.0458 0.0405
0.0383 0.0473 0.0453 0.0393 0.0486 0.0445 0.0455 0.0570 0.0496
0.0415 0.0499 0.0559 0.0541 0.0656 0.0597 0.0627 0.0692 0.0549
0.0457 0.0611 0.0572 0.0508 0.0676 0.0587 0.0636 0.0794 0.0665
0.0491 0.0581 0.0687 0.0650 0.0815 0.0725 0.0811 0.0872 0.0685
0.0638 0.0678 0.0741 0.0655 0.0787 0.0749 0.0777 0.0886 0.0858
0 Kommentare
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!