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

조회 수: 1 (최근 30일)
Hi, I'm getting this error when I index into an image using roi_5, the contents of which are shown below. 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?
I've tried to divide by the 10e-09 factor but this hasn't fixed anything.
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

답변 (1개)

KSSV
KSSV 2021년 2월 5일
The error is not due to 10e-09. In the present, you have not shown us the complete code, so we cannot help you exactly. But this error occurs when you try to use indices as negative or zero. Rememebr, in MATLAB the indices are always positive integers and logicals; if any other is used, you will get an error.
Example:
A = rand(10,1) ;
A(1) % no error
A(-1) % error, index cannot be negative
A(0) % error, index cannot be zero
A(0.5) % erorr, index cannot be a fraction
So, in your code you have to see where you got the error, go to that line and check the index value.
  댓글 수: 2
William Harris
William Harris 2021년 2월 5일
Hi KSSV, thanks for your reply.
As far as I understand, the index values are positive, as defined in the line
roi_5 = R1rho(22:30, 28:36);
The first instance I call this variable is the first instance I get the error:
y_data_cat5 = squeeze(mean(mean(data_cat(roi_5, :), 1), 2))
The full code is here:
addpath('NifTI_2040122')
addpath('Data')
load_dir = 'C:\Users\p42026wh\Dropbox (Personal)\Programming\Matlab\MRI\Data\Phantoms\210040\20210203_101135_210040_1_1\'
%define the repeats to average over
% TSL0_imnum = [1 4]
% R1rhoweighted_imnum = [2:3 5:6]
TSL0_imnum = [1 4]
R1rhoweighted_imnum = [2:3 5:6]
R1rho(22:30, 28:36) = roi_5
R1rho(36:44, 14:22) = roi_10
R1rho(41:49, 50:58) = roi_25
R1rho(24:31, 48:56) = roi_50
R1rho(24:31, 48:56) = roi_250
%TSL = 10 ms image
data_10 = load_nii([load_dir '24\im.nii']);
im_10 = rot90(permute(data_10.img, [1 3 2]), 1);
im_10_trunc = im_10(:,:,4:end);
im_10_trunc_ave = cat(3, mean(im_10_trunc(:,:,TSL0_imnum),3), mean(im_10_trunc(:,:,R1rhoweighted_imnum),3))
norm_im_10_trunc_ave = cat(3, mean(im_10_trunc(:,:,TSL0_imnum),3), mean(im_10_trunc(:,:,R1rhoweighted_imnum),3))./mean(im_10_trunc(:,:,TSL0_imnum),3)
data_20 = load_nii([load_dir '25\im.nii']);
im_20 = rot90(permute(data_20.img, [1 3 2]), 1);
im_20_trunc = im_20(:,:,4:end)
im_20_trunc_ave = cat(3, mean(im_20_trunc(:,:,TSL0_imnum),3), mean(im_20_trunc(:,:,R1rhoweighted_imnum),3))
norm_im_20_trunc_ave = cat(3, mean(im_20_trunc(:,:,TSL0_imnum),3), mean(im_20_trunc(:,:,R1rhoweighted_imnum),3))./mean(im_20_trunc(:,:,TSL0_imnum),3)
data_30 = load_nii([load_dir '26\im.nii']);
im_30 = rot90(permute(data_30.img, [1 3 2]), 1);
im_30_trunc = im_30(:,:,4:end);
im_30_trunc_ave = cat(3, mean(im_30_trunc(:,:,TSL0_imnum),3), mean(im_30_trunc(:,:,R1rhoweighted_imnum),3))
norm_im_30_trunc_ave = cat(3, mean(im_30_trunc(:,:,TSL0_imnum),3), mean(im_30_trunc(:,:,R1rhoweighted_imnum),3))./mean(im_30_trunc(:,:,TSL0_imnum),3)
data_40 = load_nii([load_dir '27\im.nii']);
im_40 = rot90(permute(data_40.img, [1 3 2]), 1);
im_40_trunc = im_40(:,:,4:end);
im_40_trunc_ave = cat(3, mean(im_40_trunc(:,:,TSL0_imnum),3), mean(im_40_trunc(:,:,R1rhoweighted_imnum),3))
norm_im_40_trunc_ave = cat(3, mean(im_40_trunc(:,:,TSL0_imnum),3), mean(im_40_trunc(:,:,R1rhoweighted_imnum),3))./mean(im_40_trunc(:,:,TSL0_imnum),3)
data_50 = load_nii([load_dir '28\im.nii']);
im_50 = rot90(permute(data_50.img, [1 3 2]), 1);
im_50_trunc = im_50(:,:,4:end);
im_50_trunc_ave = cat(3, mean(im_50_trunc(:,:,TSL0_imnum),3), mean(im_50_trunc(:,:,R1rhoweighted_imnum),3))
norm_im_50_trunc_ave = cat(3, mean(im_50_trunc(:,:,TSL0_imnum),3), mean(im_50_trunc(:,:,R1rhoweighted_imnum),3))./mean(im_50_trunc(:,:,TSL0_imnum),3)
data_60 = load_nii([load_dir '33\im.nii']);
im_60 = rot90(permute(data_60.img, [1 3 2]), 1);
im_60_trunc = im_60(:,:,4:end);
im_60_trunc_ave = cat(3, mean(im_60_trunc(:,:,TSL0_imnum),3), mean(im_60_trunc(:,:,R1rhoweighted_imnum),3))
norm_im_60_trunc_ave = cat(3, mean(im_60_trunc(:,:,TSL0_imnum),3), mean(im_60_trunc(:,:,R1rhoweighted_imnum),3))./mean(im_60_trunc(:,:,TSL0_imnum),3)
data_70 = load_nii([load_dir '34\im.nii']);
im_70 = rot90(permute(data_70.img, [1 3 2]), 1);
im_70_trunc = im_70(:,:,4:end);
im_70_trunc_ave = cat(3, mean(im_70_trunc(:,:,TSL0_imnum),3), mean(im_70_trunc(:,:,R1rhoweighted_imnum),3))
norm_im_70_trunc_ave = cat(3, mean(im_70_trunc(:,:,TSL0_imnum),3), mean(im_70_trunc(:,:,R1rhoweighted_imnum),3))./mean(im_70_trunc(:,:,TSL0_imnum),3)
data_cat = cat(3, im_10_trunc_ave(:,:,2), im_20_trunc_ave(:,:,2), im_30_trunc_ave(:,:,2), im_40_trunc_ave(:,:,2), im_50_trunc_ave(:,:,2), im_60_trunc_ave(:,:,2), im_70_trunc_ave(:,:,2))
norm_data_cat = cat(3, norm_im_10_trunc_ave(:,:,2), norm_im_20_trunc_ave(:,:,2), norm_im_30_trunc_ave(:,:,2), norm_im_40_trunc_ave(:,:,2), norm_im_50_trunc_ave(:,:,2), norm_im_60_trunc_ave(:,:,2), norm_im_70_trunc_ave(:,:,2))
imshow4(data_cat, 1, 1, 'norm')
imshow4(norm_data_cat, 1, 1, 'norm')
x_data_cat = [0:10:70]
y_data_cat5 = squeeze(mean(mean(data_cat(roi_5, :), 1), 2))
y_data_cat10 = squeeze(mean(mean(data_cat(roi_10, :), 1), 2))
y_data_cat25 = squeeze(mean(mean(data_cat(roi_25, :), 1), 2))
y_data_cat50 = squeeze(mean(mean(data_cat(roi_50, :), 1), 2))
y_data_cat250 = squeeze(mean(mean(data_cat(roi_250, :), 1), 2))
y_norm_data_cat5 = squeeze(mean(mean(norm_data_cat(roi_5, :), 1), 2))
y_norm_data_cat10 = squeeze(mean(mean(norm_data_cat(roi_10, :), 1), 2))
y_norm_data_cat25 = squeeze(mean(mean(norm_data_cat(roi_25, :), 1), 2))
y_norm_data_cat50 = squeeze(mean(mean(norm_data_cat(roi_50, :), 1), 2))
y_norm_data_cat250 = squeeze(mean(mean(norm_data_cat(roi_250, :), 1), 2))
figure(1)
plot(x_data_cat, y_data_cat5);
hold on
plot(x_data_cat, y_data_cat10);
hold on
plot(x_data_cat, y_data_cat25);
hold on
plot(x_data_cat, y_data_cat50);
hold on
plot(x_data_cat, y_data_cat250);
hold off
legend('5', '10', '25', '50', '250')

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by