I am not sure if I understand the question. Please attach some of your data and code as an example if the following does not help:
1) Instead of using a for loop, can you not just do element-wise multiplication between the matrices?
maximize(real(trace( theta1 .* Hs .* W * Hs' )))
2) If Hs is a changing part of a for loop, then you two options:
for i = 1:6
for j = 1:6
maximize(real(trace( theta1(i,j)*Hs*W(i,j)*Hs' )))
for i = 1:36
maximize(real(trace( theta1(i)*Hs*W(i)*Hs' )))
You may want to look at how linear indexing works before using the second option. For instance, in a 6-by-6 matrix, theta1(7) would be the first element of the second row (same as theta1(1,2)).