Matlab Codgen eig() function - strange behaviour - matlab

First, don't be fooled by the long post, there is not a lot of code just an observation of results so there are few example matrices.
This is a bit related to this question: Matlab Codegen Eig Function - Is this a Bug?
I know that mex/C/C++ translated eig() function may not return the same eigenvectors when using the same function in MATLAB and that's fine, but i am puzzled with results I'm getting.
First this simple example:
Output
% c = diagonal matrix of eigenvalues
% b = matrix whose columns are the corresponding right eigenvectors
function [ b, c ] = eig_test(a)
[b, c] = eig(a);
end
Running as is for [b,c] = eig_test(magic(5)) this returns:
b =
-0.4472 0.0976 -0.6330 0.6780 -0.2619
-0.4472 0.3525 0.5895 0.3223 -0.1732
-0.4472 0.5501 -0.3915 -0.5501 0.3915
-0.4472 -0.3223 0.1732 -0.3525 -0.5895
-0.4472 -0.6780 0.2619 -0.0976 0.6330
c =
65.0000 0 0 0 0
0 -21.2768 0 0 0
0 0 -13.1263 0 0
0 0 0 21.2768 0
0 0 0 0 13.1263
Translating that to mex function and running eig_test_mex(magic(5)) returns:
b =
0.4472 + 0.0000i 0.0976 + 0.0000i -0.6330 + 0.0000i 0.6780 + 0.0000i -0.2619 + 0.0000i
0.4472 + 0.0000i 0.3525 + 0.0000i 0.5895 + 0.0000i 0.3223 + 0.0000i -0.1732 + 0.0000i
0.4472 + 0.0000i 0.5501 + 0.0000i -0.3915 + 0.0000i -0.5501 + 0.0000i 0.3915 + 0.0000i
0.4472 + 0.0000i -0.3223 + 0.0000i 0.1732 + 0.0000i -0.3525 + 0.0000i -0.5895 + 0.0000i
0.4472 + 0.0000i -0.6780 + 0.0000i 0.2619 + 0.0000i -0.0976 + 0.0000i 0.6330 + 0.0000i
c =
65.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -21.2768 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i -13.1263 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 21.2768 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 13.1263 + 0.0000i
Now here the values are actually the same (with exception of first vector that differs only in - sign) but from where are these complex imaginary parts coming from?
Second example:
Using the same function as above, the input matrix is now this one below instead of magic(5):
input_matrix =
0.0440 -0.0000 0.0486 0.0752 0.0848 0.0881 0.0883 0.0874 0.0856 0.0832 0.0805 0.0775 0.0742 0.0709 0.0676 0.0644 0.0612 0.0580 0.0548 0.0518 0.0487
-0.0000 0 -0.0000 -0.0000 0 -0.0000 -0.0000 0 0 -0.0000 -0.0000 -0.0000 0 0 -0.0000 -0.0000 0 0 0 -0.0000 0
0.0486 -0.0000 0.1253 0.1231 0.1128 0.1028 0.0940 0.0867 0.0803 0.0747 0.0697 0.0651 0.0609 0.0570 0.0535 0.0503 0.0473 0.0446 0.0421 0.0397 0.0375
0.0752 -0.0000 0.1231 0.3049 0.2850 0.2641 0.2454 0.2297 0.2157 0.2034 0.1924 0.1820 0.1723 0.1636 0.1556 0.1482 0.1414 0.1351 0.1292 0.1237 0.1186
0.0848 0 0.1128 0.2850 0.3941 0.3685 0.3451 0.3253 0.3077 0.2921 0.2780 0.2646 0.2521 0.2407 0.2303 0.2207 0.2119 0.2036 0.1959 0.1887 0.1819
0.0881 -0.0000 0.1028 0.2641 0.3685 0.4420 0.4161 0.3943 0.3746 0.3571 0.3413 0.3262 0.3120 0.2990 0.2871 0.2762 0.2661 0.2566 0.2478 0.2396 0.2318
0.0883 -0.0000 0.0940 0.2454 0.3451 0.4161 0.4705 0.4474 0.4266 0.4079 0.3910 0.3748 0.3595 0.3455 0.3326 0.3208 0.3098 0.2996 0.2900 0.2811 0.2726
0.0874 0 0.0867 0.2297 0.3253 0.3943 0.4474 0.4918 0.4701 0.4507 0.4330 0.4160 0.3999 0.3851 0.3716 0.3591 0.3474 0.3366 0.3265 0.3170 0.3080
0.0856 0 0.0803 0.2157 0.3077 0.3746 0.4266 0.4701 0.5067 0.4868 0.4686 0.4511 0.4343 0.4190 0.4049 0.3919 0.3798 0.3686 0.3580 0.3481 0.3387
0.0832 -0.0000 0.0747 0.2034 0.2921 0.3571 0.4079 0.4507 0.4868 0.5182 0.4997 0.4817 0.4645 0.4487 0.4343 0.4209 0.4084 0.3968 0.3859 0.3757 0.3660
0.0805 -0.0000 0.0697 0.1924 0.2780 0.3413 0.3910 0.4330 0.4686 0.4997 0.5269 0.5087 0.4911 0.4751 0.4603 0.4466 0.4339 0.4220 0.4108 0.4003 0.3905
0.0775 -0.0000 0.0651 0.1820 0.2646 0.3262 0.3748 0.4160 0.4511 0.4817 0.5087 0.5317 0.5140 0.4977 0.4827 0.4688 0.4559 0.4438 0.4325 0.4218 0.4117
0.0742 0 0.0609 0.1723 0.2521 0.3120 0.3595 0.3999 0.4343 0.4645 0.4911 0.5140 0.5337 0.5173 0.5021 0.4881 0.4750 0.4628 0.4514 0.4406 0.4304
0.0709 0 0.0570 0.1636 0.2407 0.2990 0.3455 0.3851 0.4190 0.4487 0.4751 0.4977 0.5173 0.5351 0.5199 0.5057 0.4926 0.4803 0.4687 0.4578 0.4475
0.0676 -0.0000 0.0535 0.1556 0.2303 0.2871 0.3326 0.3716 0.4049 0.4343 0.4603 0.4827 0.5021 0.5199 0.5362 0.5220 0.5087 0.4963 0.4847 0.4737 0.4634
0.0644 -0.0000 0.0503 0.1482 0.2207 0.2762 0.3208 0.3591 0.3919 0.4209 0.4466 0.4688 0.4881 0.5057 0.5220 0.5370 0.5237 0.5112 0.4995 0.4885 0.4781
0.0612 0 0.0473 0.1414 0.2119 0.2661 0.3098 0.3474 0.3798 0.4084 0.4339 0.4559 0.4750 0.4926 0.5087 0.5237 0.5376 0.5251 0.5134 0.5023 0.4918
0.0580 0 0.0446 0.1351 0.2036 0.2566 0.2996 0.3366 0.3686 0.3968 0.4220 0.4438 0.4628 0.4803 0.4963 0.5112 0.5251 0.5381 0.5263 0.5152 0.5047
0.0548 0 0.0421 0.1292 0.1959 0.2478 0.2900 0.3265 0.3580 0.3859 0.4108 0.4325 0.4514 0.4687 0.4847 0.4995 0.5134 0.5263 0.5384 0.5273 0.5167
0.0518 -0.0000 0.0397 0.1237 0.1887 0.2396 0.2811 0.3170 0.3481 0.3757 0.4003 0.4218 0.4406 0.4578 0.4737 0.4885 0.5023 0.5152 0.5273 0.5387 0.5281
0.0487 0 0.0375 0.1186 0.1819 0.2318 0.2726 0.3080 0.3387 0.3660 0.3905 0.4117 0.4304 0.4475 0.4634 0.4781 0.4918 0.5047 0.5167 0.5281 0.5389
Running for [b,c] = eig_test(input_matrix) returns:
b =
0.0000 -0.0085 -0.0117 -0.0166 -0.0251 -0.0442 0.1334 0.9260 -0.1493 0.0548 -0.0283 0.0382 0.0170 0.0977 0.1285 -0.1697 0.1100 -0.1149 0.0635 0.0881 0.0424
1.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000
-0.0000 0.0020 0.0029 0.0042 0.0067 0.0125 -0.0404 -0.2919 0.0495 -0.0178 0.0219 0.0179 0.1086 0.2330 0.4679 -0.5139 0.4286 -0.3220 0.2241 0.1454 0.0392
0.0000 0.0001 0.0001 0.0002 0.0003 0.0005 -0.0010 -0.0102 -0.0017 -0.0143 -0.0484 -0.1305 -0.2829 -0.4495 -0.4237 0.0949 0.2592 -0.3986 0.4161 0.3145 0.1072
-0.0000 0.0001 0.0002 0.0003 0.0006 0.0005 -0.0076 -0.0267 0.0241 0.0641 0.1763 0.3391 0.4702 0.3538 -0.0858 0.3847 -0.2103 -0.1113 0.3607 0.3698 0.1531
0.0000 0.0001 0.0001 0.0003 -0.0001 0.0044 0.0173 -0.0280 -0.0740 -0.2070 -0.3832 -0.4693 -0.2586 0.1790 0.3224 0.0909 -0.3636 0.1799 0.2003 0.3654 0.1849
0.0000 0.0001 0.0001 -0.0000 0.0029 -0.0168 -0.0758 0.0155 0.2178 0.4038 0.4549 0.1676 -0.2686 -0.2836 0.1914 -0.2398 -0.2029 0.3295 0.0210 0.3288 0.2079
-0.0000 0.0001 0.0000 0.0015 -0.0114 0.0623 0.1958 -0.0857 -0.3987 -0.4488 -0.1154 0.3136 0.2223 -0.2549 -0.1457 -0.2976 0.0628 0.3254 -0.1323 0.2755 0.2255
-0.0000 0.0001 0.0005 -0.0058 0.0419 -0.1658 -0.3782 0.0966 0.4538 0.1163 -0.3266 -0.1826 0.2893 0.0922 -0.2891 -0.1140 0.2566 0.2114 -0.2394 0.2128 0.2385
0.0000 0.0001 -0.0020 0.0224 -0.1169 0.3338 0.4695 -0.1012 -0.1655 0.3142 0.1810 -0.3027 -0.0583 0.2923 -0.1581 0.1266 0.3013 0.0487 -0.2951 0.1468 0.2482
-0.0000 -0.0003 0.0085 -0.0675 0.2572 -0.4725 -0.2826 -0.0124 -0.2658 -0.2251 0.2927 0.0580 -0.2952 0.1782 0.0838 0.2657 0.2098 -0.1089 -0.3025 0.0811 0.2552
0.0000 0.0016 -0.0282 0.1667 -0.4258 0.4018 -0.1483 0.0743 0.2952 -0.2511 -0.1046 0.3011 -0.1748 -0.0861 0.2409 0.2463 0.0445 -0.2242 -0.2699 0.0183 0.2593
-0.0000 -0.0060 0.0796 -0.3248 0.4873 -0.0371 0.3582 -0.0471 0.1556 0.1904 -0.3026 0.1397 0.1135 -0.2564 0.2258 0.1072 -0.1212 -0.2793 -0.2087 -0.0393 0.2613
-0.0000 0.0202 -0.1839 0.4791 -0.2780 -0.3330 -0.0369 -0.0733 -0.2823 0.2701 -0.0569 -0.1694 0.2707 -0.2224 0.0779 -0.0674 -0.2333 -0.2741 -0.1307 -0.0908 0.2618
0.0000 -0.0569 0.3438 -0.4801 -0.1422 0.2530 -0.3217 0.0344 -0.1598 -0.0783 0.2433 -0.2745 0.1829 -0.0400 -0.1011 -0.1994 -0.2669 -0.2193 -0.0462 -0.1354 0.2611
0.0000 0.1366 -0.4999 0.2085 0.3815 0.1931 0.0665 0.0563 0.2298 -0.2886 0.2299 -0.1025 -0.0438 0.1536 -0.2162 -0.2443 -0.2244 -0.1314 0.0362 -0.1730 0.2594
-0.0000 -0.2750 0.5187 0.2125 -0.1234 -0.2906 0.3002 -0.0330 0.2275 -0.0976 -0.0389 0.1510 -0.2225 0.2429 -0.2221 -0.1981 -0.1272 -0.0288 0.1102 -0.2036 0.2569
0.0000 0.4544 -0.2791 -0.3911 -0.2949 -0.1393 -0.0141 -0.0736 -0.1126 0.2031 -0.2494 0.2586 -0.2356 0.1924 -0.1303 -0.0881 -0.0057 0.0716 0.1710 -0.2273 0.2536
-0.0000 -0.5901 -0.1434 0.0927 0.2187 0.2750 -0.2874 -0.0090 -0.2727 0.2455 -0.2023 0.1516 -0.0947 0.0451 0.0094 0.0433 0.1096 0.1561 0.2158 -0.2445 0.2496
0.0000 0.5431 0.4134 0.3143 0.2317 0.1604 -0.0958 0.0563 -0.0617 0.0119 0.0294 -0.0645 0.0962 -0.1161 0.1372 0.1548 0.1950 0.2157 0.2434 -0.2554 0.2451
-0.0000 -0.2286 -0.2287 -0.2288 -0.2283 -0.2265 0.2263 0.0338 0.2163 -0.2220 0.2231 -0.2228 0.2210 -0.2154 0.2075 0.2171 0.2365 0.2458 0.2538 -0.2607 0.2401
c =
-0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0.0063 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0.0076 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0.0089 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0.0105 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0.0123 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0.0144 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0.0157 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0.0171 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0.0203 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0.0244 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0.0298 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0.0368 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0.0464 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0581 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0762 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1063 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1746 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3324 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0781 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7.1136
And running mex version for [b,c] = eig_test_mex(input_matrix) returns:
b=
0.0424+0.0000i 0.0881+0.0000i 0.0635+0.0000i -0.1149+0.0000i 0.11+0.0000i -0.1697+0.0000i 0.1285+0.0000i 0.0977+0.0000i 0.017+0.0000i 0.0382+0.0000i -0.0283+0.0000i 0.0548+0.0000i 0.1493+0.0000i -0.926+0.0000i -0.1334+0.0000i 0.0085+0.0000i -0.0442+0.0000i -0.0117+0.0000i -0.0251+0.0000i -0.0166+0.0000i 0+0.0000i
+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i 0+0.0000i -1+0.0000i
0.0392+0.0000i 0.1454+0.0000i 0.2241+0.0000i -0.322+0.0000i 0.4286+0.0000i -0.5139+0.0000i 0.4679+0.0000i 0.233+0.0000i 0.1086+0.0000i 0.0179+0.0000i 0.0219+0.0000i -0.0178+0.0000i -0.0495+0.0000i 0.2919+0.0000i 0.0404+0.0000i -0.002+0.0000i 0.0125+0.0000i 0.0029+0.0000i 0.0067+0.0000i 0.0042+0.0000i 0+0.0000i
0.1072+0.0000i 0.3145+0.0000i 0.4161+0.0000i -0.3986+0.0000i 0.2592+0.0000i 0.0949+0.0000i -0.4237+0.0000i -0.4495+0.0000i -0.2829+0.0000i -0.1305+0.0000i -0.0484+0.0000i -0.0143+0.0000i 0.0017+0.0000i 0.0102+0.0000i 0.001+0.0000i -0.0001+0.0000i 0.0005+0.0000i 0.0001+0.0000i 0.0003+0.0000i 0.0002+0.0000i 0+0.0000i
0.1531+0.0000i 0.3698+0.0000i 0.3607+0.0000i -0.1113+0.0000i -0.2103+0.0000i 0.3847+0.0000i -0.0858+0.0000i 0.3538+0.0000i 0.4702+0.0000i 0.3391+0.0000i 0.1763+0.0000i 0.0641+0.0000i -0.0241+0.0000i 0.0267+0.0000i 0.0076+0.0000i -0.0001+0.0000i 0.0005+0.0000i 0.0002+0.0000i 0.0006+0.0000i 0.0003+0.0000i 0+0.0000i
0.1849+0.0000i 0.3654+0.0000i 0.2003+0.0000i 0.1799+0.0000i -0.3636+0.0000i 0.0909+0.0000i 0.3224+0.0000i 0.179+0.0000i -0.2586+0.0000i -0.4693+0.0000i -0.3832+0.0000i -0.207+0.0000i 0.074+0.0000i 0.028+0.0000i -0.0173+0.0000i -0.0001+0.0000i 0.0044+0.0000i 0.0001+0.0000i -0.0001+0.0000i 0.0003+0.0000i 0+0.0000i
0.2079+0.0000i 0.3288+0.0000i 0.021+0.0000i 0.3295+0.0000i -0.2029+0.0000i -0.2398+0.0000i 0.1914+0.0000i -0.2836+0.0000i -0.2686+0.0000i 0.1676+0.0000i 0.4549+0.0000i 0.4038+0.0000i -0.2178+0.0000i -0.0155+0.0000i 0.0758+0.0000i -0.0001+0.0000i -0.0168+0.0000i 0.0001+0.0000i 0.0029+0.0000i 0+0.0000i 0+0.0000i
0.2255+0.0000i 0.2755+0.0000i -0.1323+0.0000i 0.3254+0.0000i 0.0628+0.0000i -0.2976+0.0000i -0.1457+0.0000i -0.2549+0.0000i 0.2223+0.0000i 0.3136+0.0000i -0.1154+0.0000i -0.4488+0.0000i 0.3987+0.0000i 0.0857+0.0000i -0.1958+0.0000i -0.0001+0.0000i 0.0623+0.0000i 0+0.0000i -0.0114+0.0000i 0.0015+0.0000i 0+0.0000i
0.2385+0.0000i 0.2128+0.0000i -0.2394+0.0000i 0.2114+0.0000i 0.2566+0.0000i -0.114+0.0000i -0.2891+0.0000i 0.0922+0.0000i 0.2893+0.0000i -0.1826+0.0000i -0.3266+0.0000i 0.1163+0.0000i -0.4538+0.0000i -0.0966+0.0000i 0.3782+0.0000i -0.0001+0.0000i -0.1658+0.0000i 0.0005+0.0000i 0.0419+0.0000i -0.0058+0.0000i 0+0.0000i
0.2482+0.0000i 0.1468+0.0000i -0.2951+0.0000i 0.0487+0.0000i 0.3013+0.0000i 0.1266+0.0000i -0.1581+0.0000i 0.2923+0.0000i -0.0583+0.0000i -0.3027+0.0000i 0.181+0.0000i 0.3142+0.0000i 0.1655+0.0000i 0.1012+0.0000i -0.4695+0.0000i -0.0001+0.0000i 0.3338+0.0000i -0.002+0.0000i -0.1169+0.0000i 0.0224+0.0000i 0+0.0000i
0.2552+0.0000i 0.0811+0.0000i -0.3025+0.0000i -0.1089+0.0000i 0.2098+0.0000i 0.2657+0.0000i 0.0838+0.0000i 0.1782+0.0000i -0.2952+0.0000i 0.058+0.0000i 0.2927+0.0000i -0.2251+0.0000i 0.2658+0.0000i 0.0124+0.0000i 0.2826+0.0000i 0.0003+0.0000i -0.4725+0.0000i 0.0085+0.0000i 0.2572+0.0000i -0.0675+0.0000i 0+0.0000i
0.2593+0.0000i 0.0183+0.0000i -0.2699+0.0000i -0.2242+0.0000i 0.0445+0.0000i 0.2463+0.0000i 0.2409+0.0000i -0.0861+0.0000i -0.1748+0.0000i 0.3011+0.0000i -0.1046+0.0000i -0.2511+0.0000i -0.2952+0.0000i -0.0743+0.0000i 0.1483+0.0000i -0.0016+0.0000i 0.4018+0.0000i -0.0282+0.0000i -0.4258+0.0000i 0.1667+0.0000i 0+0.0000i
0.2613+0.0000i -0.0393+0.0000i -0.2087+0.0000i -0.2793+0.0000i -0.1212+0.0000i 0.1072+0.0000i 0.2258+0.0000i -0.2564+0.0000i 0.1135+0.0000i 0.1397+0.0000i -0.3026+0.0000i 0.1904+0.0000i -0.1556+0.0000i 0.0471+0.0000i -0.3582+0.0000i 0.006+0.0000i -0.0371+0.0000i 0.0796+0.0000i 0.4873+0.0000i -0.3248+0.0000i 0+0.0000i
0.2618+0.0000i -0.0908+0.0000i -0.1307+0.0000i -0.2741+0.0000i -0.2333+0.0000i -0.0674+0.0000i 0.0779+0.0000i -0.2224+0.0000i 0.2707+0.0000i -0.1694+0.0000i -0.0569+0.0000i 0.2701+0.0000i 0.2823+0.0000i 0.0733+0.0000i 0.0369+0.0000i -0.0202+0.0000i -0.333+0.0000i -0.1839+0.0000i -0.278+0.0000i 0.4791+0.0000i 0+0.0000i
0.2611+0.0000i -0.1354+0.0000i -0.0462+0.0000i -0.2193+0.0000i -0.2669+0.0000i -0.1994+0.0000i -0.1011+0.0000i -0.04+0.0000i 0.1829+0.0000i -0.2745+0.0000i 0.2433+0.0000i -0.0783+0.0000i 0.1598+0.0000i -0.0344+0.0000i 0.3217+0.0000i 0.0569+0.0000i 0.253+0.0000i 0.3438+0.0000i -0.1422+0.0000i -0.4801+0.0000i 0+0.0000i
0.2594+0.0000i -0.173+0.0000i 0.0362+0.0000i -0.1314+0.0000i -0.2244+0.0000i -0.2443+0.0000i -0.2162+0.0000i 0.1536+0.0000i -0.0438+0.0000i -0.1025+0.0000i 0.2299+0.0000i -0.2886+0.0000i -0.2298+0.0000i -0.0563+0.0000i -0.0665+0.0000i -0.1366+0.0000i 0.1931+0.0000i -0.4999+0.0000i 0.3815+0.0000i 0.2085+0.0000i 0+0.0000i
0.2569+0.0000i -0.2036+0.0000i 0.1102+0.0000i -0.0288+0.0000i -0.1272+0.0000i -0.1981+0.0000i -0.2221+0.0000i 0.2429+0.0000i -0.2225+0.0000i 0.151+0.0000i -0.0389+0.0000i -0.0976+0.0000i -0.2275+0.0000i 0.033+0.0000i -0.3002+0.0000i 0.275+0.0000i -0.2906+0.0000i 0.5187+0.0000i -0.1234+0.0000i 0.2125+0.0000i 0+0.0000i
0.2536+0.0000i -0.2273+0.0000i 0.171+0.0000i 0.0716+0.0000i -0.0057+0.0000i -0.0881+0.0000i -0.1303+0.0000i 0.1924+0.0000i -0.2356+0.0000i 0.2586+0.0000i -0.2494+0.0000i 0.2031+0.0000i 0.1126+0.0000i 0.0736+0.0000i 0.0141+0.0000i -0.4544+0.0000i -0.1393+0.0000i -0.2791+0.0000i -0.2949+0.0000i -0.3911+0.0000i 0+0.0000i
0.2496+0.0000i -0.2445+0.0000i 0.2158+0.0000i 0.1561+0.0000i 0.1096+0.0000i 0.0433+0.0000i 0.0094+0.0000i 0.0451+0.0000i -0.0947+0.0000i 0.1516+0.0000i -0.2023+0.0000i 0.2455+0.0000i 0.2727+0.0000i 0.009+0.0000i 0.2874+0.0000i 0.5901+0.0000i 0.275+0.0000i -0.1434+0.0000i 0.2187+0.0000i 0.0927+0.0000i 0+0.0000i
0.2451+0.0000i -0.2554+0.0000i 0.2434+0.0000i 0.2157+0.0000i 0.195+0.0000i 0.1548+0.0000i 0.1372+0.0000i -0.1161+0.0000i 0.0962+0.0000i -0.0645+0.0000i 0.0294+0.0000i 0.0119+0.0000i 0.0617+0.0000i -0.0563+0.0000i 0.0958+0.0000i -0.5431+0.0000i 0.1604+0.0000i 0.4134+0.0000i 0.2317+0.0000i 0.3143+0.0000i 0+0.0000i
0.2401+0.0000i -0.2607+0.0000i 0.2538+0.0000i 0.2458+0.0000i 0.2365+0.0000i 0.2171+0.0000i 0.2075+0.0000i -0.2154+0.0000i 0.221+0.0000i -0.2228+0.0000i 0.2231+0.0000i -0.222+0.0000i -0.2163+0.0000i -0.0338+0.0000i -0.2263+0.0000i 0.2286+0.0000i -0.2265+0.0000i -0.2287+0.0000i -0.2283+0.0000i -0.2288+0.0000i 0+0.0000i
c=
7.1136 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1.0781 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0.3324 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0.1746 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0.1063 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0.0762 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0.0581 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0.0464 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0.0368 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0.0298 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0.0244 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0.0203 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0.0171 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0.0157 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0144 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0063 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0123 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0076 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0105 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0089 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Ok, so the complex imaginary part is here again (i removed it from matrix c for clarity) but notice something. In eigenvalue matrix c there are the same values again but in reverse order. Why is that? At first glance is seems that values in b matrices are also the same just columns are reversed, but there are some differences. But as i understand this is ok.
Finally
I'm confused manly with two things. One is this transformation to complex numbers and the other one is this reverse ordering.
Does anyone have some insight on whats going on. Could first phenomena be resolved with updated MATLAB version or tool-chain? (I'm using MATLAB 2013, GCC 4.6 on Ubuntu 12.04). Second phenomena can probably be explained with math and implemented algorithms to compute eigenvectors/values. Any possible explanation?

There is not only one valid answer for a eigenvalue problem. Whenever dealing with such cases, best practice is to use or define a canonical form to convert equivalent answers to identical answers. To convert any answer into such a canonical form, I would apply the following steps:
Normalize all eigenvectors to length 1 (example: [-0.4472,-0.4472,-0.4472,0.4472,0.4472]' instead of [-1,-1,-1,1,1]'). Could be achieved using b=bsxfun(#rdivide,b,sqrt(sum(b.^2,1)))
For each eigenvectors with a negative value in the first component, take the negative value. (example: [0.4472,0.4472,0.4472,-0.4472,-0.4472]' instead of [-0.4472,-0.4472,-0.4472,0.4472,0.4472]'). Could be achieved using b=bsxfun(#times,sign(b(1,:)),b)
Sort eigenvectors and eigenvalues in ascending order of the eigenvalues. Could be achieved using this code
Please note that this is just one canonical form, not the canonical form. There might be canonical forms established but I have not found any reference so I basically put together what we discussed above to define a canonical form. Mathematically, applying such a canonical form results in a unique solution. In practice you will observe minor differences because of floating point precision errors.

Related

MATLAB - How do I extract the molecules from an SDF file with an ID in another text file, into a new SDF file?

I have an SDF file with thousands of molecules and several text files of ID's grouped together by certain characteristics. Right now, I have a script that loads in an CSV database with the features of the molecules and generates the ID text files by classifying based on these features. I want to use these text files to parse the SDF file to get new SDF files with the corresponding molecules. In addition, I want to do this in MATLAB.
For example, here are some molecules in the original SDF file:
NCGC00178831-03
Marvin 07111412562D
34 37 0 0 0 0 999 V2000
4.8814 -2.7443 0.0000 Cl 0 5 0 0 0 0 0 0 0 0 0 0
2.8647 -2.4751 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8647 -1.6501 0.0000 N 0 3 0 0 0 0 0 0 0 0 0 0
3.5808 -1.2318 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2970 -1.6501 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.0017 -1.2318 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7179 -1.6501 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
5.0017 -0.4068 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2970 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5808 -0.4068 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8647 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1485 -0.4068 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1485 -1.2318 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4324 -1.6501 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7162 -1.2318 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 -1.6501 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
0.7162 -0.4068 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4324 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8761 -3.5407 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
3.5923 -3.9590 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.3084 -3.5407 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.0132 -3.9590 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7293 -3.5407 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
5.0132 -4.7840 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.3084 -5.1908 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5923 -4.7840 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8761 -5.1908 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1599 -4.7840 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1599 -3.9590 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4438 -3.5407 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7276 -3.9590 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0115 -3.5407 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
0.7276 -4.7840 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4438 -5.1908 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2 3 1 0 0 0 0
3 4 2 0 0 0 0
3 13 1 0 0 0 0
4 5 1 0 0 0 0
4 10 1 0 0 0 0
5 6 2 0 0 0 0
6 7 1 0 0 0 0
6 8 1 0 0 0 0
8 9 2 0 0 0 0
9 10 1 0 0 0 0
10 11 2 0 0 0 0
11 12 1 0 0 0 0
12 13 2 0 0 0 0
12 18 1 0 0 0 0
13 14 1 0 0 0 0
14 15 2 0 0 0 0
15 16 1 0 0 0 0
15 17 1 0 0 0 0
17 18 2 0 0 0 0
19 20 2 0 0 0 0
19 29 1 0 0 0 0
20 21 1 0 0 0 0
20 26 1 0 0 0 0
21 22 2 0 0 0 0
22 23 1 0 0 0 0
22 24 1 0 0 0 0
24 25 2 0 0 0 0
25 26 1 0 0 0 0
26 27 2 0 0 0 0
27 28 1 0 0 0 0
28 29 2 0 0 0 0
28 34 1 0 0 0 0
29 30 1 0 0 0 0
30 31 2 0 0 0 0
31 32 1 0 0 0 0
31 33 1 0 0 0 0
33 34 2 0 0 0 0
M CHG 2 1 -1 3 1
M END
> <Formula>
C27H25ClN6
> <FW>
468.9806 (35.4535+224.2805+209.2465)
> <DSSTox_CID>
25848
> <SR-HSE>
0
$$$$
NCGC00166114-03
Marvin 07111412562D
31 32 0 0 0 0 999 V2000
4.9884 -1.2417 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.9884 -2.0696 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2748 -2.4764 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2748 -3.7038 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.9884 -4.1178 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7021 -3.7038 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.4157 -4.1178 0.0000 O 0 5 0 0 0 0 0 0 0 0 0 0
5.7021 -2.8760 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
4.9884 -4.9385 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2748 -5.3524 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5612 -4.9385 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5612 -4.1178 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5612 -2.0696 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5612 -1.2417 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2748 -0.8279 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
2.8403 -0.8279 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1267 -1.2417 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1267 -2.0696 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8403 -2.4764 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4202 -2.4764 0.0000 Br 0 0 0 0 0 0 0 0 0 0 0 0
1.4202 -0.8279 0.0000 O 0 5 0 0 0 0 0 0 0 0 0 0
2.8403 0.0000 0.0000 Br 0 0 0 0 0 0 0 0 0 0 0 0
5.7021 -2.4764 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.4229 -2.0696 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.4229 -1.2417 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7021 -0.8279 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7021 0.0000 0.0000 Br 0 0 0 0 0 0 0 0 0 0 0 0
7.1366 -0.8279 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
7.1366 -2.4764 0.0000 Br 0 0 0 0 0 0 0 0 0 0 0 0
7.0866 -4.1963 0.0000 Na 0 3 0 0 0 0 0 0 0 0 0 0
0.0000 -0.7708 0.0000 Na 0 3 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
1 15 1 0 0 0 0
1 26 2 0 0 0 0
2 3 2 0 0 0 0
2 23 1 0 0 0 0
3 4 1 0 0 0 0
3 13 1 0 0 0 0
4 5 2 0 0 0 0
4 12 1 0 0 0 0
5 6 1 0 0 0 0
5 9 1 0 0 0 0
6 7 1 0 0 0 0
6 8 2 0 0 0 0
9 10 2 0 0 0 0
10 11 1 0 0 0 0
11 12 2 0 0 0 0
13 14 2 0 0 0 0
13 19 1 0 0 0 0
14 15 1 0 0 0 0
14 16 1 0 0 0 0
16 17 2 0 0 0 0
16 22 1 0 0 0 0
17 18 1 0 0 0 0
17 21 1 0 0 0 0
18 19 2 0 0 0 0
18 20 1 0 0 0 0
23 24 2 0 0 0 0
24 25 1 0 0 0 0
24 29 1 0 0 0 0
25 26 1 0 0 0 0
25 28 2 0 0 0 0
26 27 1 0 0 0 0
M CHG 4 7 -1 21 -1 30 1 31 1
M END
> <Formula>
C20H6Br4Na2O5
> <FW>
691.8542 (645.8757+22.9892+22.9892)
> <DSSTox_CID>
5234
> <SR-HSE>
0
$$$$
NCGC00263563-01
Marvin 07111412562D
71 76 0 0 1 0 999 V2000
2.1953 -4.9878 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
3.6803 -4.9878 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
2.9701 -5.4074 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
6.5858 -4.9878 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
5.1008 -4.9878 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
2.1953 -4.1484 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
11.8157 -5.6335 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
14.1239 -5.8755 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
11.0893 -5.1008 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
3.6803 -4.1484 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
10.2015 -5.1008 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
12.5905 -5.1653 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
14.9633 -5.8755 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
4.3905 -5.4074 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
5.8755 -5.4074 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
2.9701 -3.6803 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
11.4606 -4.3905 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
13.6558 -5.1653 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
9.5559 -5.5043 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
7.2476 -5.5043 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
5.1008 -4.1484 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
1.4850 -5.4074 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
11.8157 -2.4858 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7.9578 -4.9878 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
6.5858 -4.1484 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
12.5905 -2.9055 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
12.3483 -4.3905 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
11.8157 -1.6626 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.8755 -3.6803 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
13.3008 -1.6626 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
12.5905 -1.2429 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
13.3008 -2.4858 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
8.8457 -4.9878 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
11.4606 -3.1961 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
14.1239 -4.5035 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0
0.7748 -4.9878 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
15.4314 -5.2137 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0
14.9633 -4.5035 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
9.9756 -4.2776 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 -5.4074 0.0000 O 0 5 0 0 0 0 0 0 0 0 0 0
7.6673 -4.2776 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1953 -5.7464 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
6.8764 -4.2776 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
9.0877 -4.2776 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7748 -4.1484 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
14.5437 -6.4567 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
3.6803 -3.3736 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
2.9701 -2.9055 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
5.8755 -2.9055 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
14.0110 -1.2429 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
12.5905 -0.4197 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
1.4850 -3.6803 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
15.5444 -6.4082 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10.5566 -4.3905 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.3905 -6.1177 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.5035 -3.7933 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
8.1838 -4.2776 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
14.0110 -2.9055 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.6558 -3.7449 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
16.1416 -5.2137 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2130 -2.9701 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1953 -2.3729 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
14.7858 -1.6626 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
13.3008 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
11.0893 -5.8755 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0
12.5905 -5.9885 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0
8.8941 -5.7464 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0
3.6803 -5.7464 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0
5.1008 -5.7464 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0
13.6558 -5.9885 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0
0.4681 -6.7634 0.0000 N 0 3 0 0 0 0 0 0 0 0 0 0
1 3 1 0 0 0 0
1 6 1 0 0 0 0
1 22 1 6 0 0 0
1 42 1 1 0 0 0
2 3 1 0 0 0 0
2 14 1 0 0 0 0
2 68 1 1 0 0 0
2 10 1 0 0 0 0
4 15 1 0 0 0 0
4 20 1 1 0 0 0
4 43 1 0 0 0 0
4 25 1 0 0 0 0
5 14 1 0 0 0 0
5 15 1 0 0 0 0
5 21 1 0 0 0 0
5 69 1 1 0 0 0
6 16 1 0 0 0 0
6 52 1 1 0 0 0
7 9 1 0 0 0 0
7 12 1 0 0 0 0
8 18 1 0 0 0 0
8 13 1 0 0 0 0
9 11 1 0 0 0 0
9 17 1 0 0 0 0
9 65 1 6 0 0 0
10 16 1 0 0 0 0
10 47 1 1 0 0 0
11 19 1 0 0 0 0
11 54 1 6 0 0 0
11 39 1 0 0 0 0
12 18 1 0 0 0 0
12 66 1 1 0 0 0
12 27 1 0 0 0 0
13 46 1 1 0 0 0
13 53 1 6 0 0 0
13 37 1 0 0 0 0
14 55 1 1 0 0 0
16 48 1 6 0 0 0
17 27 1 0 0 0 0
17 34 1 1 0 0 0
18 35 1 0 0 0 0
18 70 1 1 0 0 0
19 33 1 0 0 0 0
20 24 1 0 0 0 0
21 29 1 0 0 0 0
21 56 1 6 0 0 0
22 36 1 0 0 0 0
23 34 1 0 0 0 0
23 26 1 0 0 0 0
23 28 1 0 0 0 0
24 33 1 0 0 0 0
24 57 1 6 0 0 0
24 41 1 0 0 0 0
25 29 1 0 0 0 0
26 32 1 0 0 0 0
28 31 1 0 0 0 0
29 49 1 1 0 0 0
30 31 1 0 0 0 0
30 50 1 1 0 0 0
30 32 1 0 0 0 0
31 51 1 6 0 0 0
32 58 1 6 0 0 0
33 44 1 0 0 0 0
33 67 1 6 0 0 0
35 38 1 0 0 0 0
35 59 1 1 0 0 0
36 40 1 0 0 0 0
36 45 2 0 0 0 0
37 38 1 0 0 0 0
37 60 1 1 0 0 0
39 44 1 0 0 0 0
41 43 1 0 0 0 0
47 61 1 0 0 0 0
48 62 1 0 0 0 0
50 63 1 0 0 0 0
51 64 1 0 0 0 0
M CHG 2 40 -1 71 1
M END
> <Formula>
C47H83NO17
> <FW>
934.1584 (916.1205+18.0379)
> <DSSTox_CID>
28909
> <SR-HSE>
0
$$$$
And here are some ID's from the text file:
NCGC00015959-03
NCGC00168261-01
NCGC00257010-01
NCGC00254654-01
NCGC00254471-01
The generated SDF file should start like this:
NCGC00015959-03
Marvin 07111412562D
25 30 0 0 0 0 999 V2000
3.4098 -1.3130 0.0000 N 0 3 0 0 0 0 0 0 0 0 0 0
4.8329 -1.3130 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.4098 -2.1380 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.1248 -2.5436 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.6948 -2.5436 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.8329 -2.1380 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.1248 -0.8937 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.5547 -0.8937 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.9799 -2.1380 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.6948 -3.3548 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.2718 -2.5436 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.2718 -3.3548 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.1248 -3.3548 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.9799 -3.7741 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.5547 -2.5436 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.2765 -1.3130 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7128 -0.0894 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
0.4881 -2.2755 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
0.4881 -3.6160 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
6.8746 -0.7562 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
6.5378 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 -2.9423 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.4098 -3.7741 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.2765 -2.1380 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.6948 -0.8937 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 3 1 0 0 0 0
1 7 2 0 0 0 0
1 25 1 0 0 0 0
2 7 1 0 0 0 0
2 6 2 0 0 0 0
2 8 1 0 0 0 0
3 4 2 0 0 0 0
3 5 1 0 0 0 0
4 13 1 0 0 0 0
4 6 1 0 0 0 0
5 9 1 0 0 0 0
5 10 2 0 0 0 0
6 15 1 0 0 0 0
8 16 2 0 0 0 0
8 17 1 0 0 0 0
9 11 2 0 0 0 0
10 14 1 0 0 0 0
10 23 1 0 0 0 0
11 18 1 0 0 0 0
11 12 1 0 0 0 0
12 14 2 0 0 0 0
12 19 1 0 0 0 0
13 23 2 0 0 0 0
15 24 2 0 0 0 0
16 20 1 0 0 0 0
16 24 1 0 0 0 0
17 21 1 0 0 0 0
18 22 1 0 0 0 0
19 22 1 0 0 0 0
20 21 1 0 0 0 0
M CHG 1 1 1
M END
> <Formula>
C20H14NO4
> <FW>
332.3289
> <DSSTox_CID>
25204
> <NR-AR>
0
> <NR-ER-LBD>
1
> <NR-AhR>
1
$$$$
NCGC00168261-01
Marvin 07111412562D
23 25 0 0 0 0 999 V2000
2.1236 -2.4895 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4205 -2.0662 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.1236 -3.3074 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4205 -3.7235 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
0.7174 -2.4895 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7174 -3.3074 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8554 -2.0662 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 -2.0662 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4205 -1.2412 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8554 -3.7235 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5656 -2.4895 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.5656 -3.3074 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8554 -1.2412 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
0.7174 -0.8251 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 -1.2412 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0430 -2.8984 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7174 -4.1324 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.2902 -3.7378 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7174 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.0292 -3.3145 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.4569 -3.3360 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7538 -3.7378 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7.1743 -3.7378 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
1 3 2 0 0 0 0
1 7 1 0 0 0 0
2 5 2 0 0 0 0
2 9 1 0 0 0 0
3 4 1 0 0 0 0
3 10 1 0 0 0 0
4 6 1 0 0 0 0
5 8 1 0 0 0 0
5 6 1 0 0 0 0
6 16 1 0 0 0 0
6 17 1 0 0 0 0
7 11 2 0 0 0 0
7 13 1 0 0 0 0
8 15 2 0 0 0 0
9 14 2 0 0 0 0
10 12 2 0 0 0 0
11 12 1 0 0 0 0
12 18 1 0 0 0 0
14 15 1 0 0 0 0
14 19 1 0 0 0 0
18 20 1 0 0 0 0
20 22 1 0 0 0 0
21 22 1 0 0 0 0
21 23 1 0 0 0 0
M END
> <Formula>
C21H26O2
> <FW>
310.4299
> <DSSTox_CID>
28922
> <NR-AR>
0
> <NR-AhR>
1
> <SR-MMP>
1
$$$$
NCGC00257010-01
Marvin 07111412562D
35 37 0 0 0 0 999 V2000
2.0286 -3.5779 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7.0019 -7.8578 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7.0019 -0.7019 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.8589 -3.5779 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
1.6092 -2.8589 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
1.6092 -4.2799 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
3.2784 -4.2799 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
6.5825 -7.1217 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.5825 -1.4381 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.3681 -3.5779 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.5024 -3.5779 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.5024 -4.9989 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.0915 -4.2799 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.3412 -3.5779 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.3412 -4.9989 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7704 -4.2799 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7704 -2.8589 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7.7294 -1.1385 0.0000 F 0 0 0 0 0 0 0 0 0 0 0 0
6.2829 -0.2996 0.0000 F 0 0 0 0 0 0 0 0 0 0 0 0
7.7294 -7.4213 0.0000 F 0 0 0 0 0 0 0 0 0 0 0 0
7.4384 -8.5597 0.0000 F 0 0 0 0 0 0 0 0 0 0 0 0
6.2829 -8.2601 0.0000 F 0 0 0 0 0 0 0 0 0 0 0 0
7.4384 0.0000 0.0000 F 0 0 0 0 0 0 0 0 0 0 0 0
7.0019 -2.1485 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7.0019 -6.4112 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7607 -1.4381 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7607 -7.1217 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7607 -5.7008 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.7607 -2.8589 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.5825 -5.7008 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6.5825 -2.8589 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.3412 -6.4112 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.3412 -2.1485 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 -2.9103 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0086 -4.2542 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 4 2 0 0 0 0
1 5 1 0 0 0 0
1 6 1 0 0 0 0
2 8 1 0 0 0 0
2 20 1 0 0 0 0
2 21 1 0 0 0 0
2 22 1 0 0 0 0
3 9 1 0 0 0 0
3 18 1 0 0 0 0
3 19 1 0 0 0 0
3 23 1 0 0 0 0
4 7 1 0 0 0 0
5 17 1 0 0 0 0
6 16 1 0 0 0 0
7 13 2 0 0 0 0
8 27 1 0 0 0 0
8 25 2 0 0 0 0
9 26 2 0 0 0 0
9 24 1 0 0 0 0
10 16 1 0 0 0 0
10 34 1 0 0 0 0
10 35 1 0 0 0 0
10 17 1 0 0 0 0
11 13 1 0 0 0 0
11 14 2 0 0 0 0
12 13 1 0 0 0 0
12 15 2 0 0 0 0
14 29 1 0 0 0 0
15 28 1 0 0 0 0
24 31 2 0 0 0 0
25 30 1 0 0 0 0
26 33 1 0 0 0 0
27 32 2 0 0 0 0
28 30 2 0 0 0 0
28 32 1 0 0 0 0
29 31 1 0 0 0 0
29 33 2 0 0 0 0
M END
> <Formula>
C25H24F6N4
> <FW>
494.4753
> <DSSTox_CID>
3868
> <NR-AR>
0
> <NR-ER>
1
> <NR-AhR>
1
$$$$
I've seen this post: Extract molecules in order from SDF file according to IDs given in another file that offers a solution in unix to solve this. I've used that workaround in the command line:
awk 'BEGIN{ORS="$$$$"}NR==FNR{a[$1]=$0;next}$1 in a' ids.txt RS="$" molecules.sdf > molecules_by_ids.sdf and was able to get most of what I wanted. However, even when I use this command-line option I am not able to get 100% of the molecules extracted from the SDF file. For example, there are 981 molecules positive for one of the features, the text file gets 981 ID's, and this command gives me 950 molecules in the SDF file.
What I really want is a MATLAB solution that does not miss any of the molecules in the generated file. I appreciate any efforts to make a solution. Thanks!
A workaround I found in MATLAB is the following function, where "id" is the name of the ID TXT file, "sdfs" is the SDF database, and "sdf_name" is the name of the new SDF file with the molecules extracted by ID:
function write_sdf(id, sdfs, sdf_name)
% Open the text file of ids.
fid = fopen(id);
% Convert the sdf file to a character array.
data = fileread(sdfs);
% For each id, get the portion of the sdf file corresponding
% to the molecule id.
while true
mol_id = fgetl(fid);
mol_full = '';
% When we're at the end of the file, leave the loop.
if mol_id == -1
% We're done with the id file.
fclose(fid);
break;
else
mol_after = extractAfter(data, mol_id);
mol_between = extractBefore(mol_after, '$$$$');
mol_full = [char(mol_id) char(mol_between) '$$$$'];
% Write the molecule to the sdf file.
writelines(mol_full, sdf_name, WriteMode='append');
end
end
end
The problem with this solution is that it is VERY slow. If someone knows a faster way to do this please let me know! For now, I will be using this.

How to reorder the rows and columns of a matrix multiplying a vector indexed by (i,j)

I am trying to construct a particular matrix A which multiplies a column vector v= [p_0,0; p_0,1; ... p_0,N; p_1,0; ...; p_N,N].
I know the required matrix (B say) if the vector v was rearranged in the order given by sorting by the second index before the first ( i.e if v were [p_0,0; p_1,0; ... p_N,0; p_0,1; ...; p_N,N]), however would like to rearrange the rows and columns of this matrix to obtain A so that it multiplies the correctly ordered v.
Example:
B =
-2.6667 1.0000 0 0 0 0 0 0 0
-0.5000 0 0.5000 0 0 0 0 0 0
0 -1.0000 -0.4706 0 0 0 0 0 0
0 0 0 -2.6667 1.0000 0 0 0 0
0 0 0 -0.5000 0 0.5000 0 0 0
0 0 0 0 -1.0000 -0.4706 0 0 0
0 0 0 0 0 0 -2.6667 1.0000 0
0 0 0 0 0 0 -0.5000 0 0.5000
0 0 0 0 0 0 0 -1.0000 -0.4706
multiplying v in the wrong order
p_0,0
p_1,0
p_2,0
p_0,1
p_1,1
p_2,1
p_0,2
p_1,2
p_2,2
As long as I understood, you want to convert matrix B to the matrix A in the following order:
v1= [p_0,0; p_0,1; ... p_0,N; p_1,0; ...; p_N,N]
v2=[p_0,0; p_1,0; ... p_N,0; p_0,1; ...; p_N,N]
where A*v1 equals to B*v2.
For this, you need swapping of each row of the matrix.
Try this code:
m=length(B); % m is equal to NxN
N=sqrt(m);
A=zeros(m,m);
for i=0:N-1
for j=0:N-1
A(j+i*N+1,:)=B(i+N*j+1,:);
end
end

Project schedule plot in Matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to make some changes in the plot done by my senior, in Java. And since I do not know that language, I want to do it in Matlab. I have attached both the data (text format) and plot here. It is the output of project schedule where the value represents the cost associated with task (link) and their continuation in time (say task 2 is done from 6th to 9th month and cost is 0.665m $).
Value 0 indicate that there is no work in that time period for a particular task (row). The plot in Java, shows the variation in time and cost for each task.
I have been trying to make similar kind of plot (bar in case of the circles) in Matlab but could not.
Link# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 0 0 0 0 0 0 0 0 0 0 0 0 0.34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0.665 0.665 0.665 0.665 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0.34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0.36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0.14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0.57 0.57 0.57 0.57 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8 0.64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0.58 0.58 0.58 0.58 0.58 0.58 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 0.24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11 0.56 0.56 0.56 0.56 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12 0.2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
13 0 0 0 0 0 0 0 0 0.56 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.62 0 0 0 0 0 0 0 0 0 0 0
15 0 0 0 0.582857143 0.582857143 0.582857143 0.582857143 0.582857143 0.582857143 0.582857143 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16 0.536 0.536 0.536 0.536 0.536 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
17 0.2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
18 0 0.6 0.6 0.6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
19 0 0 0 0 0.0000 0 0 0 0 0 0 0 0.606666667 0.606666667 0.606666667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
20 0 0 0 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
21 0 0 0.606666667 0.606666667 0.606666667 0.606666667 0.606666667 0.606666667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
22 0 0 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
23 0 0 0 0 0 0 0 0.58 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
24 0 0 0 0 0 0 0 0 0 0.651428571 0.651428571 0.651428571 0.651428571 0.651428571 0.651428571 0.651428571 0 0 0 0 0 0 0 0 0 0 0 0 0 0
25 0 0 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
26 0 0 0 0 0 0 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
27 0.546666667 0.546666667 0.546666667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
28 0 0.655 0.655 0.655 0.655 0.655 0.655 0.655 0.655 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
29 0.5 0.5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
30 0 0 0 0 0 0 0 0 0 0.44 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
31 0.54 0.54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
32 0 0.64 0.64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
33 0 0 0 0 0 0 0 0 0 0.611428571 0.611428571 0.611428571 0.611428571 0.611428571 0.611428571 0.611428571 0 0 0 0 0 0 0 0 0 0 0 0 0 0
34 0 0 0 0 0 0 0 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
35 0.36 0.36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
36 0 0 0 0 0 0 0 0 0 0 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0 0 0 0 0 0 0 0 0 0 0 0
37 0.38 0.38 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
38 0.613333333 0.613333333 0.613333333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
39 0.58 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
40 0 0 0 0 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0 0 0 0
java plot
Here is an example for a solution, assuming data is all what is within the chart (i.e., no axis tick labels):
Links_num = size(data,1);
[y,x] = ndgrid(1:Links_num,1:size(data,2));
c = repelem(1:Links_num,size(data,2),1).';
scatter(x(:),y(:),data(:)*100+1,c(:),'filled')
clr = lines(Links_num);
colormap(clr)
title('Time Period')
ax = gca;
ax.XAxisLocation = 'top';
ax.XAxis.TickValues(1) = [];
clr = lines(Links_num); % make a color map
ax.YAxis.Direction = 'reverse';
links = [repmat('Link ',Links_num,1) num2str((1:Links_num).')];
x = repelem(ax.XAxis.Limits(1)-0.05,Links_num); % make an x position vector
for k = 1:Links_num
text(ax.XAxis.Limits(1)-0.05,k,links(k,:),'Color',clr(k,:),...
'HorizontalAlignment','right');
end
ax.YAxis.Visible = 'off'; % remove the original labels
The result:

DGESV gives wrong solution?

all. I'm trying to solve a Ax=B system in fortran by calling DGESV from lapack.
where,
A =[
1 0 1 0 1 0 1
0 1 0 1 0 1 0
0 0 4 0 32 0 108
0 0 0 24 0 120 0
0 0 0 0 48 0 192
0 0 0 0 0 80 0
0 0 0 0 0 0 120]
B=[
0
0
2
6
0
0
0]
x can be simply calculated in Matlab by x=A\B, which gives
x =
-0.5000
-0.2500
0.5000
0.2500
0
0
0
while I doing the same thing in Fortran, it gives me totally different values. Is there anything wrong with my code or I just make errors in calling DGESV?
Here is my fortran code:
program GG
implicit none
integer, parameter :: N=7
integer :: i,j,ipiv(N),ok
real(8), dimension(1:N,1:N) :: M
real(8), dimension(1,1:N) :: b
M(:,1)=(/1.,0.,1.,0.,1.,0.,1./)*1.d0
M(:,2)=(/0.,1.,0.,1.,0.,1.,0./)*1.d0
M(:,3)=(/0.,0.,2.**3/2,0.,4.**3/2,0.,6.**3/2/)*1.d0
M(:,4)=(/0.,0.,0.,3.*(3.**2-1.),0.,5.*(5.**2-1.),0./)*1.d0
M(:,5)=(/0.,0.,0.,0.,4.*(4.**2-2.**2),0.,6.*(6.**2-2.**2)/)*1.d0
M(:,6)=(/0.,0.,0.,0.,0.,5.*(5.**2-3.**2),0./)*1.d0
M(:,7)=(/0.,0.,0.,0.,0.,0.,6.*(6.**2-4.**2)/)*1.d0
b=reshape((/0,0,2,6,0,0,0/)*1.d0,shape(b))
call DGESV(N,1,M,N,ipiv,b,N,ok)
write(*,*), b
end program GG
the results given by this code is:
0.0000000000000000 0.0000000000000000 0.50000000000000000 0.25000000000000000 -0.33333333333333337 -0.37500000000000000 8.3333333333333315E-002
thanks, sincerely.
The matrix you are creating in Fortran appears to be the transpose (rows and columns flipped) of the one you define in MATLAB. Note (in MATLAB):
>> A = [1 0 1 0 1 0 1
0 1 0 1 0 1 0
0 0 4 0 32 0 108
0 0 0 24 0 120 0
0 0 0 0 48 0 192
0 0 0 0 0 80 0
0 0 0 0 0 0 120];
>> B = [0 0 2 6 0 0 0];
>> x = A\B.'
x =
-0.5000
-0.2500
0.5000
0.2500
0
0
0
>> x = (A.')\B.' % A is transposed, and you get your Fortran result
x =
0
0
0.5000
0.2500
-0.3333
-0.3750
0.0833
In other words, doing M(:,1)=... in Fortran fills the first column, not the first row. If you flip them to M(1,:)=... and so forth, I think it should match the results from MATLAB.

Neural network with matlab

I'm trying to make a neural network that can make a facial recognition. I have a database of 15 pictures each person. And 5 pictures each person that needs to be recognized without being in the database.
The first part is the function generate_input
function[NNinput]=generate_input(image,imagesize)
imag_in=imread(image); %inlezen van de afbeelding
imgray=rgb2gray(imag_in); %Omzetting van rgb naar grijswaardenafbeelding
imag_double=double(imgray); %casting naar een double
inverse_gray=abs(imag_double -255); %inverteren van de grijswaarden
imag_int=uint8(inverse_gray); %double naar int
imag_resize=imresize(imag_int,[imagesize imagesize]);
[row,col]=size(imag_resize);
NNinput=zeros(1,(row*col));
NNindex=1;
for rowcounter=1:row
for colcounter=1:col
NNinput(1,NNindex)=imag_resize(rowcounter,colcounter);
NNindex=NNindex+1;
end
end
NNinput=log10(abs(fft(NNinput)));
The second part is the where the network is created
clear all;
persons=10; %aantal personen
trainingset=15; %grootte van de trainingset per persoon
imsize=50; %grootte van de nieuwe (te analyseren) afbeeldingen
NNinput=zeros(imsize^2,1);
targets=zeros(10,1);
inputrange=zeros(imsize^2,2);
targetrange=zeros(persons,2);
%generating NNinputs (NNinputs is the input database of all the pictures
%Generating targets
for personcounter=1:trainingset
S1_im =cat(2,'S1_' ,num2str(personcounter),'.jpg');
S2_im =cat(2,'S2_' ,num2str(personcounter),'.jpg');
S3_im =cat(2,'S3_' ,num2str(personcounter),'.jpg');
S4_im =cat(2,'S4_' ,num2str(personcounter),'.jpg');
S5_im =cat(2,'S5_' ,num2str(personcounter),'.jpg');
S6_im =cat(2,'S6_' ,num2str(personcounter),'.jpg');
S7_im =cat(2,'S7_' ,num2str(personcounter),'.jpg');
S8_im =cat(2,'S8_' ,num2str(personcounter),'.jpg');
S9_im =cat(2,'S9_' ,num2str(personcounter),'.jpg');
S10_im=cat(2,'S10_',num2str(personcounter),'.jpg');
S1_mat =generate_input(S1_im ,imsize);
S2_mat =generate_input(S2_im ,imsize);
S3_mat =generate_input(S3_im ,imsize);
S4_mat =generate_input(S4_im ,imsize);
S5_mat =generate_input(S5_im ,imsize);
S6_mat =generate_input(S6_im ,imsize);
S7_mat =generate_input(S7_im ,imsize);
S8_mat =generate_input(S8_im ,imsize);
S9_mat =generate_input(S9_im ,imsize);
S10_mat=generate_input(S10_im,imsize);
if (personcounter == 1)
NNinput(:,personcounter+0)=S1_mat';
targets(:,personcounter+0)=[1 0 0 0 0 0 0 0 0 0]';
NNinput(:,personcounter+1)=S2_mat';
targets(:,personcounter+1)=[0 1 0 0 0 0 0 0 0 0]';
NNinput(:,personcounter+2)=S3_mat';
targets(:,personcounter+2)=[0 0 1 0 0 0 0 0 0 0]';
NNinput(:,personcounter+3)=S4_mat';
targets(:,personcounter+3)=[0 0 0 1 0 0 0 0 0 0]';
NNinput(:,personcounter+4)=S5_mat';
targets(:,personcounter+4)=[0 0 0 0 1 0 0 0 0 0]';
NNinput(:,personcounter+5)=S6_mat';
targets(:,personcounter+5)=[0 0 0 0 0 1 0 0 0 0]';
NNinput(:,personcounter+6)=S7_mat';
targets(:,personcounter+6)=[0 0 0 0 0 0 1 0 0 0]';
NNinput(:,personcounter+7)=S8_mat';
targets(:,personcounter+7)=[0 0 0 0 0 0 0 1 0 0]';
NNinput(:,personcounter+8)=S9_mat';
targets(:,personcounter+8)=[0 0 0 0 0 0 0 0 1 0]';
NNinput(:,personcounter+9)=S10_mat';
targets(:,personcounter+9)=[0 0 0 0 0 0 0 0 0 1]';
else
NNinput=cat(2,NNinput,S1_mat');
targets=cat(2,targets,[1 0 0 0 0 0 0 0 0 0]');
NNinput=cat(2,NNinput,S2_mat');
targets=cat(2,targets,[0 1 0 0 0 0 0 0 0 0]');
NNinput=cat(2,NNinput,S3_mat');
targets=cat(2,targets,[0 0 1 0 0 0 0 0 0 0]');
NNinput=cat(2,NNinput,S4_mat');
targets=cat(2,targets,[0 0 0 1 0 0 0 0 0 0]');
NNinput=cat(2,NNinput,S5_mat');
targets=cat(2,targets,[0 0 0 0 1 0 0 0 0 0]');
NNinput=cat(2,NNinput,S6_mat');
targets=cat(2,targets,[0 0 0 0 0 1 0 0 0 0]');
NNinput=cat(2,NNinput,S7_mat');
targets=cat(2,targets,[0 0 0 0 0 0 1 0 0 0]');
NNinput=cat(2,NNinput,S8_mat');
targets=cat(2,targets,[0 0 0 0 0 0 0 1 0 0]');
NNinput=cat(2,NNinput,S9_mat');
targets=cat(2,targets,[0 0 0 0 0 0 0 0 1 0]');
NNinput=cat(2,NNinput,S10_mat');
targets=cat(2,targets,[0 0 0 0 0 0 0 0 0 1]');
end
end
%Generating inputrange
for inputrangecounter=1:imsize^2
inputrange(inputrangecounter,:)=[0 255];
end
%Generating targetrange
for targetrangecounter=1:persons
targetrange(targetrangecounter,:)=[0 1];
end
%Creating the neural network
net = feedforwardnet(100,'trainscg');
net = configure(net,inputrange,targetrange);
net.trainParam.epochs = 10000;
net.trainParam.max_fail =400;
net=trainscg(net,NNinput,targets,'useGPU','only');
and the last part of the file is testing the network by inserting a picture in the network
test_im=cat(2,'S5_2.jpg'); %Name of the picture
test_mat=generate_input(test_im,imsize); %Create inputvector from picture
Output=net(test_mat); %put inputvector in the network
plot(Output) %plot the output
It doesn't matter which input I give, the output stays the same.