How Solve equation having Matrix parameters in Matlab - matlab

I want to solve the following equation for X. All parameters in my equation are matrices:
( [A]' * [X] )+( [X] * [A] ) = -I
I =
0 0 0 0
0 0 0 0
0 0 6.7955 -2.8529
0 0 -2.8529 3.9426
and
[A] =
-0.0038 -0.0011 -0.0012 -0.0012
-0.0011 -0.0049 -0.0012 -0.0023
1.0000 0 0 0
0 1.0000 0 0

You can use sylvester:
>> sylvester(A', A, -I)
ans =
1.0e+06 *
2.2772 -1.4202 0.0071 -0.0045
-1.4202 0.9749 -0.0043 0.0032
0.0071 -0.0043 0.0011 -0.0005
-0.0045 0.0032 -0.0005 0.0005

Related

for loops and matrices in matlab

I am trying to link two equations, where I use a for loop calculate the value of k from the range of frequencies (eg. 1-5 Hz) then use each of the k values and substitute the values of k into a 6x6 matrix. Can anyone help show me how to create a matrix for each value of k in Matlab?
1st Equation
for f = 1:5; % Range of Frequencies (Hz)
f;
w = 2.*pi.*f; % Angular Frequency (Hz)
p = 8050;% Density of Mild Steel(kg/m^3)
v = 0.30; % Poissons Ratio of Mild Steel
R = 0.02; % Radius of Pipe (m)
E = 210*10^9; % Youngs Modulus of Mild Steel (pa)
a = (w.^2).*p;
b = (p.*(1-(v.^2)).*(R.^2).*(w.^2)-E);
c = (p.*(R.^2).*(w.^2)-E).*E;
**k(f) = sqrt((a.*b)/c); % k = Wave Number**
end
2nd Equation (6x6 Matrix)
k =
L1=0.1;
L2=0.6;
L3=0.6;
D= [0,0,exp(-k*L1),exp(-k*L2),0,0; exp(-k*L1),1,exp(-k*L1),exp(-k*L2),0,0; -k*exp(-k*L1),k,k*exp(-k*L1),-k*exp(-k*L2),0,0;0,0,exp(-k*(L1+L2)),k,-exp(-k*(L1+L2)),-exp(-k*L3);0,0,-k*exp(-k*(L1+L2)),1,k*exp(-k*(L1+L2)),k*exp(-k*L3);0,0,exp(-k*(L1+L2)),1,0,0]
(Answer modified following the comment)
You can try definig the output array D as a 3 dimensions array.
D=NaN(6,6,length(k))
for i=1:length(k)
D(:,:,i)= [0,0,exp(-k(i)*L1),exp(-k(i)*L2),0,0; exp(-k(i)*L1),1,exp(-k(i)*L1),exp(-k(i)*L2),0,0; -k(i)*exp(-k(i)*L1),k(i),k(i)*exp(-k(i)*L1),-k(i)*exp(-k(i)*L2),0,0;0,0,exp(-k(i)*(L1+L2)),k(i),-exp(-k(i)*(L1+L2)),-exp(-k(i)*L3);0,0,-k(i)*exp(-k(i)*(L1+L2)),1,k(i)*exp(-k(i)*(L1+L2)),k(i)*exp(-k(i)*L3);0,0,exp(-k(i)*(L1+L2)),1,0,0]
end
The D array will be:
D(:,:,1) =
0 0 0.9999 0.9993 0 0
0.9999 1.0000 0.9999 0.9993 0 0
-0.0012 0.0012 0.0012 -0.0012 0 0
0 0 0.9991 0.0012 -0.9991 -0.9993
0 0 -0.0012 1.0000 0.0012 0.0012
0 0 0.9991 1.0000 0 0
D(:,:,2) =
0 0 0.9998 0.9985 0 0
0.9998 1.0000 0.9998 0.9985 0 0
-0.0025 0.0025 0.0025 -0.0025 0 0
0 0 0.9983 0.0025 -0.9983 -0.9985
0 0 -0.0025 1.0000 0.0025 0.0025
0 0 0.9983 1.0000 0 0
D(:,:,3) =
0 0 0.9996 0.9978 0 0
0.9996 1.0000 0.9996 0.9978 0 0
-0.0037 0.0037 0.0037 -0.0037 0 0
0 0 0.9974 0.0037 -0.9974 -0.9978
0 0 -0.0037 1.0000 0.0037 0.0037
0 0 0.9974 1.0000 0 0
D(:,:,4) =
0 0 0.9995 0.9971 0 0
0.9995 1.0000 0.9995 0.9971 0 0
-0.0049 0.0049 0.0049 -0.0049 0 0
0 0 0.9966 0.0049 -0.9966 -0.9971
0 0 -0.0049 1.0000 0.0049 0.0049
0 0 0.9966 1.0000 0 0
D(:,:,5) =
0 0 0.9994 0.9963 0 0
0.9994 1.0000 0.9994 0.9963 0 0
-0.0061 0.0062 0.0061 -0.0061 0 0
0 0 0.9957 0.0062 -0.9957 -0.9963
0 0 -0.0061 1.0000 0.0061 0.0061
0 0 0.9957 1.0000 0 0
Hope this helps.
I will try to answer your question...
Firstly, you don't need the initial for loop. You can calculate all values of wavelength without loops.
f = 1:5; % Range of Frequencies (Hz)
w = 2.*pi.*f; % Angular Frequency (Hz)
p = 8050;% Density of Mild Steel(kg/m^3)
v = 0.30; % Poissons Ratio of Mild Steel
R = 0.02; % Radius of Pipe (m)
E = 210*10^9; % Youngs Modulus of Mild Steel (pa)
a = (w.^2).*p;
b = (p.*(1-(v.^2)).*(R.^2).*(w.^2)-E);
c = (p.*(R.^2).*(w.^2)-E).*E;
k = sqrt((a.*b)./c); % k = Wave Number
L1=0.1;
L2=0.6;
L3=0.6;
Then you can use a for loop to calculate the D matrix and a cell array to store the results:
results = cell(1, length(k));
for i = 1:length(k)
results{i} = [
0,0,exp(-k(i)*L1),exp(-k(i)*L2),0,0;...
exp(-k(i)*L1),1,exp(-k(i)*L1),exp(-k(i)*L2),0,0;...
-k(i)*exp(-k(i)*L1),k(i),k(i)*exp(-k(i)*L1),-k(i)*exp(-k(i)*L2),0,0;...
0,0,exp(-k(i)*(L1+L2)),k(i),-exp(-k(i)*(L1+L2)),-exp(-k(i)*L3);...
0,0,-k(i)*exp(-k(i)*(L1+L2)),1,k(i)*exp(-k(i)*(L1+L2)),k(i)*exp(-k(i)*L3);...
0,0,exp(-k(i)*(L1+L2)),1,0,0
]
end
P.S. Without trying to get on to you, I think that what you just want is to someone solve an excercise for you. If this is not the case, ignore my p.s.

Matlab Codgen eig() function - strange behaviour

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.

Why is there difference in the DCT coefficients between matlab and libjpeg

I need to manipulate the DCT coefficients of a JPEG image, so I got the values using libjpeg and matlab. The problem is that I am getting different values.
I am working on the following image:
libjpeg
I have used jpeg_read_coefficients to get the virtual array and then iterate over it and printed the DCT values, for the first array and first dct block I got the following values:
9 8 10 6 3 1 1 0 -6 0 1 0 0 0 0 0 6 -1 -1 0 0 0 0 0 5 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
This is the coefficients in straight order as said in the documentation.
Matlab
In order to get the coefficients I read the image and ran dct2 function on its first 8X8 block:
A = imread('test2.jpg');
dct2(A(1:8,1:8,1));
and came up with the following values(For the RGB color space):
404.7500 -2.5251 15.1704 18.4835 14.7500 8.0465 10.4933 1.6169
-50.4069 0.4634 3.2328 -0.3498 -0.6745 0.9465 0.4599 0.5694
16.0546 -1.7709 -3.3624 -0.0164 0.2310 -0.9791 -0.1250 -1.0587
14.7190 0.9301 -0.0935 0.2866 -0.5779 -0.2573 1.2260 0.1872
15.5000 0.4715 -1.2505 -0.9827 -0.0000 0.1208 0.2474 1.4739
9.4079 0.5931 -0.3263 0.0659 0.4536 0.2866 -0.3059 0.3997
10.6682 0.2906 0.1250 -0.6038 -0.0957 -0.0583 -0.8876 -1.2576
0.7863 -0.1074 -0.7177 -0.4161 0.9382 -0.6301 -0.7815 0.4634
And those values for YCbCr color space:
1.0e+03 *(
1.0305 0.0142 0.0168 0.0151 0.0130 0.0071 0.0085 0.0003
-0.0100 0.0002 0.0029 -0.0001 -0.0002 0.0006 -0.0004 -0.0005
0.0151 -0.0024 -0.0025 -0.0000 0 -0.0002 -0.0005 -0.0000
0.0132 -0.0000 -0.0004 -0.0007 -0.0008 -0.0008 -0.0001 0.0011
0.0140 -0.0004 0.0003 -0.0010 0.0005 -0.0002 0.0001 -0.0004
0.0086 0.0001 -0.0000 0.0002 0.0004 0.0004 -0.0003 0.0001
0.0078 -0.0002 -0.0005 -0.0000 0 -0.0003 -0.0000 -0.0005
0.0003 -0.0000 -0.0003 0.0003 0.0012 -0.0003 -0.0002 -0.0005)
My guess, since I couldn't approve it with the libjpeg documentation, is that the coefficients are for the YCbCr space.
I understand that the values in the libjpeg are after quantization error and the first DC component is decoded by the difference so lets take a look at the second component, its quantization constant is 2 (retrieved with hex dump from the raw data) so the difference can be at most 2.
The computed DCT value for the second coefficient in the Matlab calculation is 14.2 while in the raw image its 8. And I cant figure out what I have done wrong, any ideas and suggestions are welcome.

How to revert signed distance function back to binary image?

So I used MATLAB's bwdist function to obtain the distance transform of a binary image. My question is, how do I transform a signed distance matrix back into a binary image?
D = bwdist(BW)
Specifically, is there a transform that can go back from BW -> D?
If D = bwdist(BW), how about BW0 = D<=0?
Consider the first example from the bwdist documentation:
bw = zeros(5,5); bw(2,2) = 1; bw(4,4) = 1
bw =
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
[D,IDX] = bwdist(bw)
D =
1.4142 1.0000 1.4142 2.2361 3.1623
1.0000 0 1.0000 2.0000 2.2361
1.4142 1.0000 1.4142 1.0000 1.4142
2.2361 2.0000 1.0000 0 1.0000
3.1623 2.2361 1.4142 1.0000 1.4142
To get back your binary image, you just want the points in the distance transform that are equal to zero (i.e. on a non-zero pixel in the original bw):
>> bw0 = D<=0
bw0 =
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
>> isequal(bw,bw0)
ans =
1

Matlab: Avoid exp(-3) conversion at text data input

I have a txt file with data, so I used the following function
M = dlmread('data.txt', '\t');
But My data is getting converted to exp(-3) how do I avoid that?
Here how the data look like:
M =
1.0e+03 *
0 0.0080 0.3500 0.1500 4.6990 0.0145 0.0740 0
0 0.0080 0.4000 0.1700 4.7460 0.0120 0.0710 0
0 0.0080 0.4000 0.1750 4.3850 0.0120 0.0720 0
0 0.0060 0.2500 0.0720 3.1580 0.0195 0.0750 0
0 0.0080 0.3040 0.1500 3.8920 0.0125 0.0720 0
0 0.0080 0.3500 0.1450 4.4400 0.0140 0.0750 0
0 0.0060 0.2500 0.1050 3.8970 0.0185 0.0750 0
0 0.0060 0.1630 0.1330 3.4100 0.0158 0.0780 0.0010
0 0.0080 0.2600 0.1100 4.0600 0.0190 0.0770 0
As H Muster says, this is a display issue.
A better way (in my opinion) of displaying your data is via num2str.
If you run
num2str(M)
you will see
ans =
0 0.008 0.35 0.15 4.699 0.0145 0.074 0
0 0.008 0.4 0.17 4.746 0.012 0.071 0
0 0.008 0.4 0.175 4.385 0.012 0.072 0
0 0.006 0.25 0.072 3.158 0.0195 0.075 0
0 0.008 0.304 0.15 3.892 0.0125 0.072 0
0 0.008 0.35 0.145 4.44 0.014 0.075 0
0 0.006 0.25 0.105 3.897 0.0185 0.075 0
0 0.006 0.163 0.133 3.41 0.0158 0.078 0.001
0 0.008 0.26 0.11 4.06 0.019 0.077 0
which is probably what you were expecting in the first place.
If you want more precision, pass a format string to num2str.
For example:
num2str(M,'%8g')