Info

此问题已关闭。 请重新打开它进行编辑或回答。

Hi everybody, I have a problem with running the program .

2 次查看(过去 30 天)
Hi everybody, I have a problem with running the program .
there is an error like this ???
Index exceeds matrix dimensions.
in this four instructions
imagesTrain = reshape (images(:,:,:,indsTrain),insz(1),insz(2),[]);
imagesVal = reshape(images(:,:,:,indsVal), insz(1),insz(2),[]);
labelsTrain = reshape(labels(:,:,:,indsTrain),insz(1),insz(2),[]);
labelsVal = reshape(labels(:,:,:,indsVal), insz(1),insz(2),[]);
How is the solution of the error?
  5 个评论
Stephen23
Stephen23 2017-3-31
编辑:Stephen23 2017-3-31
mosbahi sourour's "Answer" moved here and formatted correctly:
This is the program :
functionimdb=setupImdbIBSRv2(net,indsTrain,indsVal,augment,view)
if nargin < 2, indsTrain = 1:5; end if nargin < 3, indsVal = []; end
if nargin < 4, augment = true;end
if nargin < 5, view = 1; end
% Read original patches
switch view
case {1,'axial'}
permvec = [1 2 3];
case {2, 'sagittal'}
permvec = [2 3 1];
case {3, 'coronal'}
permvec = [3 1 2];
otherwise
error('Invalid view')
end
paths = setPaths();
dirs = dir(paths.IBSR); dirs = dirs(~ismember({dirs.name},{'.','..','.DS_Store','README.txt'}));
nFiles = numel(dirs); assert(nFiles == 18);
insz = [256,128,256];
insz = insz(permvec); nChannelsPerVolume = insz(3);
images = zeros(insz(1),insz(2),nChannelsPerVolume,nFiles, 'uint8');
labels = zeros(insz(1),insz(2),nChannelsPerVolume,nFiles, 'uint8');
tmpSeg = zeros(insz(1),insz(2),nChannelsPerVolume, 'uint8');
ibsrLabels = [0,2,3,4,5,7,8,10,11,12,13,14,15,16,17,18,24,26,28,29,30,41,...
42,43,44,46,47,48,49,50,51,52,53,54,58,60,61,62,72];
labelMap = containers.Map(ibsrLabels,0:numel(ibsrLabels)-1);
ticStart = tic;
for i=1:nFiles
imgPath = fullfile(paths.IBSR, dirs(i).name, [dirs(i).name '_ana_strip.nii']);
segPath = fullfile(paths.IBSR, dirs(i).name, [dirs(i).name '_seg_ana.nii']);
if ~exist(imgPath,'file') && exist([imgPath '.gz'],'file')
gunzip([imgPath '.gz']);
end
if ~exist(segPath,'file') && exist([segPath '.gz'],'file')
gunzip([segPath '.gz']);
end
img = load_nii(imgPath);
seg = load_nii(segPath);
img.img = permute(img.img, permvec);
seg.img = permute(seg.img, permvec);
assert(size(img.img,3) == nChannelsPerVolume)
tmpSeg(:) = 0;
for j=1:numel(ibsrLabels)
tmpSeg(seg.img == ibsrLabels(j)) = labelMap(ibsrLabels(j));
end
images(:,:,:,i) = 255*bsxfun(@rdivide,single(img.img), single(max(max(img.img))));
labels(:,:,:,i) = tmpSeg;
progress('Reading ISBR images',i,nFiles,ticStart);
end
imagesTrain = reshape (images(:,:,:,indsTrain),insz(1),insz(2),[]);
imagesVal = reshape(images(:,:,:,indsVal), insz(1),insz(2),[]); clear images;
labelsTrain = reshape(labels(:,:,:,indsTrain),insz(1),insz(2),[]);
labelsVal = reshape(labels(:,:,:,indsVal), insz(1),insz(2),[]); clear labels;
assert(size(imagesTrain,3) == numel(indsTrain) * nChannelsPerVolume);
assert(size(imagesVal ,3) == numel(indsVal) * nChannelsPerVolume)
complete copy of the error message
Index exceeds matrix dimensions.
Error in cnn>setupImdbIBSRv2 (line 179)
imagesTrain = reshape (images(:,:,:,indsTrain),insz(1),insz(2),[]);
Jan
Jan 2017-3-31
And the complete error message is? We need to know at least the failing line.

回答(1 个)

John D'Errico
John D'Errico 2017-3-31
When you get the error, what is the shape of images, and insz?
size(images)
size(insz)
Also, what are the values of indsTrain and indsval at that time?
One of those variables is not the size you are expecting it to be.
I cannot answer your question, because I cannot run your code as you ran it. I don't have the input arguments, nor do I know what you passed into the function. But you can determine all of this easily.
The trick is to use the debugger. Set this flag at the command line, then run your code. That will have the debugger stop when it sees an error, and then you can do the tests I showed above. That will give you everything you need to answer the question yourself.
dbstop if error

此问题已关闭。

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by