a4 = 10*magic(4);
a5 = magic(5);
a4
a5
diag4 = sub2ind([4,4], 1:3,1:3);
diag5 = sub2ind([5,5], 1:3,1:3);
a5(diag5) = a4(diag4) #Display changed contents
diag4 %# Display diagonal of magic4
diag5 %# Display diagonal of magic5
a4(diag4)=a5(diag5) %# Recovering the original
The output is
a4 = %# Display of original a4 magic square
160 20 30 130
50 110 100 80
90 70 60 120
40 140 150 10
a5 = %#Display of original magic square
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
diag4 =
1 6 11
diag5 =
1 7 13
a5 =
160 24 1 8 15
23 110 7 14 16
4 6 60 20 22
10 12 19 21 3
11 18 25 2 9
a4 =
160 20 30 130
50 110 100 80
90 70 60 120
40 140 150 10
What is the logic behind the manner in which diag4 and diag5 have been generated?
See Question&Answers more detail:os