I'm not totally clear about your goal, still here's one way to extract the diagonals of an RGB image (diagonal of 2D matrices for each color channel):
A = rand(32,32,3); %# it can be any 3D matrix (and not necessarily square)
[r c d] = size(A);
diagIDX = bsxfun(@plus, 1:r+1:r*c, (0:d-1)'.*r*c);
A( diagIDX(:) )
diagIDX
will have three rows, each contain the (linear) indices of the diagonal elements (one for each slice). From there you can adapt it to your code...
The idea behind the above code is simple: take a 2D matrix, the diagonal elements can be accessed using:
A = rand(5,4);
[r c] = size(A);
A( 1:r+1:r*c )
then in the 3D case, I add an additional offset to reach the other slices in the same manner.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…