# How to select bottom three “norm” values and their corresponding matrices? [closed]

I calculated five random matrices of 6x3 (with some code which is already working) having name Subscript[randompop, j].Here is the output

Generation1(3574    0   0
21270   41391   0
39078   44832   0
49793   43292   6960
25878   43718   6653
3320    42756   6285

)

Generation2(38896   0   0
4824    44017   0
17859   41724   0
19252   42645   6280
16571   43909   6163
11282   41303   6045

)

Generation3(43389   0   0
24098   43342   0
25903   43755   0
13866   41213   6636
36952   41804   6449
5405    44705   6330

)

Generation4(29842   0   0
35302   40474   0
35274   41975   0
32354   43335   6223
38490   43377   6853
30391   42254   6793

)

Generation5(26649   0   0
48623   44943   0
11134   42438   0
12427   42923   6646
5791    42962   6352
4186    40010   6429

)


Now I want to calculate the error w.r.t given matrix named as "aim" by calculating the difference of each matrixSubscript[randompop, j] and matrix "aim"(where j ranges from 1 to 5) , after calculating difference I calculated the "frobenius norm". Given below the code I had made yet.

aim = {{10000, 10000, 0}, {10000, 100000, 0}, {10000, 10000,
0}, {10000, 0, 10000}, {10000, 0, 10000}, {0, 10000, 10000}};
For[a = 1, a <= 5, a++,

Subscript[error, a] = aim - Subscript[randompop, a][];

Subscript[normerror, a] = Norm[Subscript[error, a], "Frobenius"];
Print[Subscript[normerror, a]];
];


Now I want to choose the bottom three norms and their corresponding matrix. Kindly help me to proceed forward.

• Get rid of the Subscript's. They look nice, but they make calculations with lists impossible. Store lists of values as lists of values, matrices as lists of lists, lists of matrices as lists of lists of lists and so on. Use functions like Subscript or MatrixForm to pretty-print results, but never inside calculations.
• Get rid of the For loop. Rule of thumb: If you think you need a for loop, you're doing something wrong.
• If your values are proper lists, what you want is probably something simple like error = aim - randompop; or error = Norm[aim-#]& /@ randompop (honestly, I didn't try to understand what your code in detail.) See this answer for further info.
• Once you're that far, you can get the indices of the lowest values in a list using: indices = Ordering[error, 3], and then use error[[indicies]] to extract the corresponding values out of a list.