MATLAB: How to copy anti diagonal elements to another matrix

anti diagonal couple of actions two matrix

Hello, I need to copy both diagonals from one matrix to another. For example:
If (i==j or i==n+1-j) : MatA(i,j)=MatB(i,j);
else: MatA(i,j)=MatB(i,j)+1;
Thanks

Best Answer

Here is a simple solution using linear indexing. It assumes that both matrices are square and the same size.
>> A = randi(8,5)
A =
8 3 3 5 6
6 3 7 4 5
4 7 5 4 2
4 8 7 7 1
6 6 6 3 1
>> N = numel(A);
>> R = size(A,1);
>> V = [1:R+1:N,R:R-1:N];
>> B = A+1;
>> B(V) = A(V)
B =
8 4 4 6 6
7 3 8 4 6
5 8 5 5 3
5 8 8 7 2
6 7 7 4 1