# Trying to Loop through different values of two matrixes simultaneously

1 view (last 30 days)
Michael Costa on 16 Oct 2020
Answered: Matt J on 16 Oct 2020
I have a program that calculates the grain boundary energy (single output) given inputs of two 3 by 3 matrices, let's call them matrix A and matrix B. These matrices tell the program the relative orientation of two grains in the metal. I need to write another program that optimizes the grain boundary energy by finding the orientation (two matrices) that minimizes the energy. My thought of how to do this is to loop through "all" (starting off i would want each matrix element to start at zero, step by 0.01, and go to say, 10) the values of each matrix element. After all the energies (again, single outputs corresponding to each iteration of inputs A and B) are calculated, I would have MATLAB find the minimum values, say the lowest 50% of energies. So far I can't figure out how to test every combination of the elements of matrix A with every combination of the elements of matrix B. Any help or even ideas on this would be much appreciated.
Thanks
Mathieu NOE on 16 Oct 2020
hello
this is a first level description of the problem, but we need to understand from a mathematical perspective how the elements of A and B are defined , not only the range but how each element is related to the others (random distribution ? )
what are the operation(s) you have to do on the A and B matrixes to compute your energie(s) ?
a kind of pseudo code would be helpful

Matt J on 16 Oct 2020
You can form all combinations of two matrices using ndgrid,
A=rand(3), B=rand(3)
A = 3×3
0.2535 0.4016 0.2631 0.3204 0.5844 0.9297 0.6106 0.9268 0.6711
B = 3×3
0.7170 0.9237 0.3843 0.4447 0.0765 0.5473 0.0286 0.6168 0.3079
[AA,BB]=ndgrid(A,B)
AA = 9×9
0.2535 0.2535 0.2535 0.2535 0.2535 0.2535 0.2535 0.2535 0.2535 0.3204 0.3204 0.3204 0.3204 0.3204 0.3204 0.3204 0.3204 0.3204 0.6106 0.6106 0.6106 0.6106 0.6106 0.6106 0.6106 0.6106 0.6106 0.4016 0.4016 0.4016 0.4016 0.4016 0.4016 0.4016 0.4016 0.4016 0.5844 0.5844 0.5844 0.5844 0.5844 0.5844 0.5844 0.5844 0.5844 0.9268 0.9268 0.9268 0.9268 0.9268 0.9268 0.9268 0.9268 0.9268 0.2631 0.2631 0.2631 0.2631 0.2631 0.2631 0.2631 0.2631 0.2631 0.9297 0.9297 0.9297 0.9297 0.9297 0.9297 0.9297 0.9297 0.9297 0.6711 0.6711 0.6711 0.6711 0.6711 0.6711 0.6711 0.6711 0.6711
BB = 9×9
0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079 0.7170 0.4447 0.0286 0.9237 0.0765 0.6168 0.3843 0.5473 0.3079