Error: The logical indices contain a true value outside of the array bounds.

10 views (last 30 days)
Hi all,
I have amended the code below slightly and it is now returning an error when it was working perfectly before, and I can't figure out why! Any advice greatly appreciated:
The logical indices contain a true value outside of the array bounds.
Error in LF_lesion_quant (line 44)
mean(F(I)); ...
clear, clc
addpath /Applications/freesurfer/7.3.2/matlab/
datadir = '';
d = dir([datadir filesep '*LF_Lesion_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Lesion Mask ' ]);
d = dir([datadir filesep '*LF_Contralateral_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Contralateral Mask ' ]);
d = dir([datadir filesep '*LF_DWI_Nonlinear_lncc5sx30vel_Coregistration.nii.gz']);
disp(['Found ' num2str(length(d)) ' LF DWI ' ]);
d = dir([datadir filesep '*LF_FLAIR.nii.gz']);
disp(['Found ' num2str(length(d)) ' LF FLAIR ' ]);
resultspath = '';
Nd = numel(d);
data = cell(Nd+1,5);
data(1,2:6) = {'Lesion_Mask_mm3', 'Contralateral_Mask_mm2', 'Mean_Lesion_DWI', 'Mean_Lesion_FLAIR', 'Mean_Contralateral_FLAIR'};
for i = 1 : Nd
disp(sprintf('Working on case %d of %d: %s',i,Nd,d(i).name))
f = find(d(i).name=='_');
prefix = d(i).name(1:f(1));
ipsimaskfile = fullfile(datadir,[prefix 'LF_Lesion_Mask.nii.gz']);
contramaskfile = fullfile(datadir,[prefix 'LF_Contralateral_Mask.nii.gz']);
dwifile = fullfile(datadir,[prefix 'LF_DWI_Nonlinear_lncc5sx30vel_Coregistration.nii.gz']);
flairfile = fullfile(datadir,[prefix 'LF_FLAIR.nii.gz']);
IMmri = MRIread(ipsimaskfile);
CMmri = MRIread(contramaskfile);
Dmri = MRIread(dwifile);
Fmri = MRIread(flairfile);
I = IMmri.vol > (max(IMmri.vol(:))/2);
C = CMmri.vol > (max(CMmri.vol(:))/2);
D = double(Dmri.vol);
F = double(Fmri.vol);
data(i+1,:) = { ...
prefix(1:end-1); ...
sum(I(:)) * prod(IMmri.volres); ...
sum(C(:)) * prod(CMmri.volres); ...
mean(D(I)); ...
mean(F(I)); ...
mean(F(C)); ...
};
disp(data)
output_file = fullfile(resultspath,'FLAIR_SIR_NEW_2.csv');
writecell(data, output_file);
disp('All done!');
end

Answers (1)

Cris LaPierre
Cris LaPierre on 12 Oct 2023
This error occurs when your logical index has more elements than the array you are indexing with it.
ind1 = logical([0 1 1 0]);
A = rand(1,4);
% works
A(ind1)
ans = 1×2
0.3485 0.9401
% your error
ind2 = logical([0 1 1 0 1]);
A(ind2)
The logical indices contain a true value outside of the array bounds.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!