Index in position 3 exceeds array bounds (must not exceed 28).

1 view (last 30 days)
Good morning,
We have a script but we are not used to work with MATLAB, so we would like to have some advice.
The error that occurs is this one: Index in position 3 exceeds array bounds (must not exceed 28).
Error in compute_strength (line 19)
ts(i,:)=ts(i,:)+squeeze(img(x(v),y(v),z(v),:))';
And the script which we are working with is this one:
function [strength_pos, strength_neg]=compute_strength(fmri_path,atlas_path)
vola=spm_vol(atlas_path);
atlas=spm_read_vols(vola);
vol=spm_vol(fmri_path);
img=spm_read_vols(vol);
%a=size(atlas) 61x73x61 (x,y,z)
%a=size(img) 128x128x29x300 (x,y,z,t)
%1. Extraer las time series de cada region del atlas
num_regions=max(atlas(:)); %Si les regions estan etiquetades, es queda amb l'ultim valor i així sabem el total de regions.
ts=zeros(num_regions,size(img,4)); %Inicialitza la matriu temporal amb el #regions i el tamany de la dimensió temporal de la imatge.
for i=1:num_regions
indx=find(atlas==i);
[x, y, z]=ind2sub(size(atlas),indx);
mat=[x,y,z];
for v=1:length(indx)
ts(i,:)=ts(i,:)+squeeze(img(x(v),y(v),z(v),:))'; % this line is error
end
ts(i,:)=ts(i,:)./length(indx);
end
%Crear matriz funcional
func_network=corr(ts');
func_network(find(isnan(func_network)))=0;
func_network = 0.5 * log((1 + func_network)./(1 - func_network));
func_network(1:1+num_regions:end)=0;
%Density
pos_network=func_network;
pos_network(find(pos_network<0))=0;
neg_network=func_network;
neg_network(find(neg_network>0))=0;
neg_network=abs(neg_network);
strength_pos=sum(pos_network,2);
strength_neg=sum(neg_network,2);
%save('fichero.txt','strength_pos','-ascii');
Thank you very much

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 17 Jul 2020
ts(i,:)=ts(i,:)+squeeze(img(x(v),y(v),z(v),:))'; % this line is error
I think the error is caused because the value of z(v) is more than 28 & as the error says that size(img,3) is 28. Hence make sure the value of z(v) is not more that 28.
Example code to reproduce the above error:
img = rand(128,128,28,300);
img(100,50,29,:)

Tags

Community Treasure Hunt

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

Start Hunting!