I know that diagind() returns the index of the diagonal elements of matrix, in which we can assign them to a specific value such as in the below
aa=rand(9,9);
aa[diagind(aa)] .= 1
However, lets say I am working on a part of the matrix aa such as aa[5:7,5:7] and I want to assign its diagonal elements to 1 (in other words aa[5,5]=1, aa[6,6]=1, aa[7,7]=1), how can I do that?
Thank you very much! @Seif_Shebl
How is about changing the diagonal elements of aa[5:7,4:6] (i.e., aa[5,4]=1, aa[6,5]=1, aa[7,6]=1)
I tried aa[diagind(aa)[5:7,4:6]] .= 1 but it gives an error
ERROR: BoundsError: attempt to access 9-element StepRange{Int64, Int64} at index [5:7, 5:7]
Stacktrace:
Never use a comprehension when you just want a for loop. A comprehension generates an array, so if you don’t want this array it is a pointless side effect, and it’s not any less compact to write for ind in zip(5:7,4:6); aa[ind...] = 1; end or for j in 4:6; aa[j+1,j] = 1; end