Question: nested loop..please help!

hallo everyone, I'm working on a project for the Uni, and I'm facing the flowing problem.

I have Test-Matrix [3*12] called Testna it contains random nummbers( random because i wantend test the loop, the real matrix is much complexer ).

Testna := RandomMatrix(3, 12)

and I have a Vektor called Z

> Z:=Vector[row](5, {(1) = z, (2) = z-2, (3) = z-4, (4) = z-6, (5) = z-8})

>k:=4

Z and K are paramter previously in the worksheet

and I want to create a Matrix CC[3*3] using a nested loop so that the cofficient of CC(i,j) are as following

so for i =1 , j=1 and n from 1 to k=4  I wanted to have

CC(1,1):= Testna(1,1)*(Z(1,1)-Z(1,2))  +  Testna(1,4)*(Z(1,2)-Z(1,3) +Testna(1,7)*(Z(1,3)-Z(1,4))  +  Testna(1,10)*(Z(1,4)-Z(1,5)

so for i =1 , j=2 and n from 1 to k=4  I wanted to have

CC(1,2):= Testna(1,2)*(Z(1,1)-Z(1,2))  +  Testna(1,5)*(Z(1,2)-Z(1,3) +Testna(1,8)*(Z(1,3)-Z(1,4))  +  Testna(1,11)*(Z(1,4)-Z(1,5)

so for i =1 , j=3 and n from 1 to k=4  I wanted to have

CC(1,3):= Testna(1,3)*(Z(1,1)-Z(1,2))  +  Testna(1,6)*(Z(1,2)-Z(1,3) +Testna(1,9)*(Z(1,3)-Z(1,4))  +  Testna(1,12)*(Z(1,4)-Z(1,5)

so for i =2 , j=1 and n from 1 to k=4  I wanted to have

CC(2,1):= Testna(2,1)*(Z(1,1)-Z(1,2))  +  Testna(2,4)*(Z(1,2)-Z(1,3) +Testna(2,7)*(Z(1,3)-Z(1,4))  +  Testna(2,10)*(Z(1,4)-Z(1,5)

so for i =2 , j=2 and n from 1 to k=4  I wanted to have

CC(2,2):= Testna(2,2)*(Z(1,1)-Z(1,2))  +  Testna(2,5)*(Z(1,2)-Z(1,3) +Testna(2,8)*(Z(1,3)-Z(1,4))  +  Testna(2,11)*(Z(1,4)-Z(1,5)

so for i =2 , j=2 and n from 1 to k=4  I wanted to have

CC(2,3):= Testna(2,3)*(Z(1,1)-Z(1,2))  +  Testna(2,6)*(Z(1,2)-Z(1,3) +Testna(2,9)*(Z(1,3)-Z(1,4))  +  Testna(2,12)*(Z(1,4)-Z(1,5)

and so on.. for i=3, j=3 and n from 1 to K=4

the Problem is that the loop doesnt work as I wanted(above)  and I dont know why, so insted of having a Matrix CC:=[  [C(1,1), C(1,2) C(1,3),], [C(2,1), C(2,2) C(2,3),], [C(3,1), C(3,2) C(3,3)]  ] I become a Matrix CC:=[ [C(1,1), C(1,1) C(1,1),],[C(2,2), C(2,2) C(2,2),],[C(3,3), C(3,3) C(3,3),]]

here is the Maple code
 

Test := proc ()
local i, j, n; global CC, Testna, Z, k:
CC := Matrix(3, 3);
for i from 1to 3 do for
j from 1 to 3 do
for n from 1 to k do
CC(i, j) := CC(i, j)+Testna(i, 1+(n-1)*3)*(Z(1, n)-Z(1, n+1))
end do
end do
end do
end proc():
CC
please Help
Please Wait...