MATLAB: How to change a specific group of diagonal values within a matrix, but still print the whole matrix

MATLABmatrix manipulation

I have 44×22 matrix [T] and I want to apply an equation to a specifc group of cells within the matrix, which are on a diagonal with each other.
0 0 1 0 0 0 . . .
0 0 0 1 0 0 . . .
0 0 0 0 1 0 . . .
0 0 0 0 0 1 . . .
Here the 1's are an example of potential cells that I'd like to change. I know that by using the "diag" function I could change those values, but I haven't found a way to keep those changed values stored within the matrix.

Best Answer

Check this example. Suppose you have a 4x4 matrix
x = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
>> x
x =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
and you want to change this diagnal
diag([1 1 1],1)
>> diag([1 1 1],1)
ans =
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0
You can do it like this
x = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
mask = diag([1 1 1],1)==1;
x(mask) = 2*x(mask); % multiply the element on that diagonal by 2
Result:
>> x
x =
1 4 3 4
5 6 14 8
9 10 11 24
13 14 15 16