MATLAB: How to access 5 values in an array at a time

for loopMATLABmatrix array

hi everyone…im currently working on an image which i already convert it in terms of its gray level values for pixel by pixel…right now i have problem in accessing the values of those arrays….for example, an image containing 70rows (i=70) and 70columns (j=70)….let say i want to read one whole row first..but i want to read it 5 values at a time, end then the next 5, and then the next until the end…how do i do that???the reason i want to read it bit by bit is because i want to find the average of each 5 gray level values and run a statistical analysis on those averages…
I=imread('1.jpg');
D=double(I(:,:,1));
[i,j]=size(D);
n=0;
k=1;
grayval(1)=0;
for i=25:40
for j=1:40
glevel(j)=D(i,j)
grayval(k+1)=grayval(k)+glevel(j)
n=n+1;
k=k+1;
end
xbar(i)=grayval(k)/n
end
there's something wrong with my code…it returns the average of the whole j…can u please help me??tanx in advance…

Best Answer

You can use reshape() and mean() to avoid the for-loops.
A=magic(10);
% if you want to do average of 5, row-wise
B=A'; % if column-wise, there is no need to do this line, just use A
C=reshape(B,5,[])
D=mean(C)