MATLAB: Index exceed matrix dimensions

dimension errorMATLAB

hello folks, I'm trying to run 2 different dimensional vectors by using weighted regions with a chosen factor. can I get some expert opinion on identify the error,please. your help is gratefully appreciated.Many thanks in advance.
x=train_data; % 882 x2 double
z=test_data; %882 x 8 double
distance_max=[];
for h=1:size(x)
for kb=1:size(z)
kk=x(:,h);
gg=z(:,kb); <-- this is where the error appeared
weightsum=0;
ww = [0, 1,1,1,1,1,0,0,1,1,1,1,1,0,1,1,2,1,2,1,1,2,4,4,0,4,4,2,1,1,1,1,1,1,1,0,1,2,2,2,1,0,0,0,1,1,1,0,0];
length=18;
region = size(x,1)/length;
for e=1:region
e_start = (e -1) * length+1 ;
e_end = e_start + length-1 ;
xpart=kk(e_start:e_end);
zpart = gg (e_start:e_end);
D1 = pdist2(xpart',zpart','chisq')*ww(e);
distance_max=[distance_max,D1];
weightsum=weightsum+D1;
end
results(h,k)={weightsum};
end
end

Best Answer

Please try changing the line
for kb=1:size(y)
to the following:
for kb=1:size(z)
Also, please correct the issue with size described in the answer provided by Adam.