MATLAB: How to do this in a for loop in Matlab

for loopmatrices

I have a 3-dimensial matrix W(160,170,18) and wanna to compute the difference between each to sucessive matrices inside W. for example diff1 = W(:,:,1) – W(:,:,2) and diff2 = W(:,:,2) – W(:,:,3), etc …
Next I want to select some special parts of the resulting matrices, For example:
NewDiff1 = [diff1(20:50,110:140);diff1(60:90,110:140)];
and the same thing for the other matrices. finally I want to compute the mean of each matrix and the error as follow:
mean1 = mean(mean(NewDiff1)); er1 = 0.1-abs(mean1);
I successeded to do this for each matrix alone, but prefer to do all at once in a for loop. Can you please help me to do it.

Best Answer

dff = diff(W,1,3)
a = dff([20:50,60:90],110:140,:)
meanN = mean(reshape(a,[],size(a,3)));
erN = .1 - abs(meanN);