I need to operate this loop in Matlab - matlab

function stainless_steel(~,~,~)
clear,clc
T_i = [0 0 12.5 25 37.5 50 0];
k = 0.0162;
cp = 0.5;
rho = 8000;
dt = 3;
dx = 0.0125;
t = 120;
q = 20000;
Fo = (k*dt)/(((dx)^2)*rho*cp);
e_gen = q*(dt)/(rho*cp);
n = t/dt;
p = n;
T = zeros(n,7);
for iteration = 1:p
for x = 2:6
T(iteration,x) = [Fo*(T_i(1,x+1)+273.15 + T_i(1,x-1)+273.15) + (T_i(1,x)+273.15)*(1-2*Fo) + e_gen - 273.15];
end
end
T_i = T;
disp(T)
end
I'm getting this as a result:
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
0 15.9720 27.5000 40.0000 52.5000 60.1400 0
But what I need is the matrix T_i to change each time. For example, if T_i at the beginning was
[0 0 12.5 25 37.5 50 0]
I need my second T_i to be
[ 0 15.9720 27.5000 40.0000 52.5000 60.1400 0]
which is the result I got from the original T_i
And my third T_i to be whatever result I get from the second T_i being T_i and so on for 40 iterations.
But I don't know how to achieve that :(

You could solve it by using the the following lines:
T_i = zeros(n,7);
T_i(1,:) = [0 0 12.5 25 37.5 50 0];
for iteration = 1:p-1
for x = 2:6
T_i(iteration+1,x) = [Fo*(T_i(iteration,x+1)+273.15 + T_i(iteration,x-1)+273.15) + (T_i(iteration,x)+273.15)*(1-2*Fo) + e_gen - 273.15];
end
end
This makes sure that for every row you go down in your matrix, the information of the row above is used as input. I defined it as T_i but you might want to change that as it suggests that it is the initial value. The actual initial value in my example is T_i(1,:)

Related

smooth filtering shifts my original signal?

Here is my code:
sigma = 10;
sz = 20;
x = linspace(-sz / 2, sz / 2-1, sz);
gf = exp(-x .^ 2 / (2 * sigma ^ 2));
gf = gf / sum (gf); % normalize
f_filter = cconv(gf,f,length(f));
Basically I am Gaussian filtering original signal f. However, when I look at the filtered signal f_filter, there is a shift comparing the original signal f (See attached figure). I am not sure why this is happening. I would like to only smooth but not shift the orginal signal. Please help. Thanks.
my original signal f is here:
-0.0311
-0.0462
-0.0498
-0.0640
-0.0511
-0.0522
-0.0566
-0.0524
-0.0478
-0.0482
-0.0516
-0.0435
-0.0417
-0.0410
-0.0278
-0.0079
-0.0087
-0.0029
0.0105
0.0042
0.0046
0.0107
0.0119
0.0177
0.0077
0.0138
0.0114
0.0103
0.0089
0.0122
0.0122
0.0118
0.0041
0.0047
0.0062
0.0055
0.0033
0.0096
0.0062
-0.0013
0.0029
0.0112
0.0069
0.0160
0.0127
0.0131
0.0039
0.0116
0.0078
0.0018
0.0023
0.0133
0.0140
0.0135
0.0098
0.0100
0.0133
0.0131
0.0086
0.0114
0.0131
0.0175
0.0137
0.0157
0.0040
0.0136
0.0009
0.0049
0.0157
0.0104
0.0038
0.0039
0.0029
0.0126
0.0044
0.0055
0.0040
0.0091
-0.0023
0.0107
0.0151
0.0115
0.0135
0.0160
0.0071
0.0098
0.0094
0.0072
0.0079
0.0055
0.0155
0.0107
0.0108
0.0085
0.0099
0.0055
0.0078
0.0027
0.0121
0.0077
0.0062
0.0021
-0.0019
-0.0003
-0.0022
0.0059
0.0099
0.0114
0.0069
0.0038
0.0020
-0.0031
0.0024
-0.0025
-0.0004
0.0041
0.0059
0.0018
0.0033
0.0130
0.0131
0.0076
0.0084
0.0029
0.0086
0.0078
0.0054
0.0121
0.0101
0.0132
0.0115
0.0074
0.0070
0.0088
0.0017
-0.0003
-0.0060
0.0078
0.0100
0.0044
0.0017
0.0027
0.0062
0.0029
-0.0035
0.0032
0.0060
-0.0035
0.0081
0.0027
0.0043
0.0013
0.0049
0.0119
0.0273
0.0363
0.0435
0.0432
0.0357
0.0424
0.0318
0.0341
0.0354
0.0325
0.0263
0.0320
0.0312
0.0345
0.0407
0.0378
0.0376
0.0334
0.0381
0.0428
0.0375
0.0431
0.0403
0.0395
0.0308
0.0150
0.0006
0.0054
0.0002
0.0090
0.0075
0.0051
0.0067
0.0062
0.0108
0.0059
0.0095
0.0065
0.0087
0.0056
0.0136
0.0057
0.0079
0.0107
0.0106
0.0041
0.0032
0.0106
0.0091
0.0082
0.0025
0.0124
0.0035
0.0034
0.0097
0.0034
0.0050
0.0119
0.0087
0.0081
0.0118
0.0088
0.0050
0.0050
0.0057
0.0118
0.0122
0.0207
0.0112
0.0125
0.0083
0.0125
0.0140
0.0147
0.0237
0.0206
0.0141
0.0164
0.0189
0.0189
0.0136
0.0183
0.0195
0.0209
0.0154
0.0211
0.0254
0.0163
0.0249
0.0236
0.0262
0.0278
0.0285
0.0275
0.0212
0.0277
0.0211
0.0248
0.0289
0.0240
0.0266
0.0479
0.1744
0.4070
0.6818
0.8811
0.9859
0.9347
0.8441
0.7625
0.6396
0.4724
0.3639
0.3406
0.3406
0.3363
0.3318
0.3251
0.3287
0.3135
0.3122
0.3058
0.3103
0.3012
0.2974
0.2995
0.2941
0.2981
0.2968
0.2958
0.2938
0.2929
0.2926
0.2942
0.2982
0.2898
0.2940
0.2927
0.2950
0.2899
0.2979
0.2915
0.2961
0.2921
0.2931
0.2989
0.2941
0.2977
0.3041
0.3042
0.3086
0.3048
0.3069
0.3055
0.3123
0.3138
0.3128
0.3115
0.3092
0.3174
0.3152
0.3106
0.3080
0.3166
0.3109
0.3103
0.3135
0.3101
0.3133
0.3147
0.3044
0.2980
0.2972
0.3013
0.2980
0.3069
0.3932
0.6593
0.8921
1.1071
1.2763
1.3947
1.5076
1.6278
1.7452
1.7993
1.8287
1.8470
1.8957
1.9408
1.9791
2.0272
2.0686
2.0974
2.1335
2.1790
2.2134
2.2545
2.2903
2.3163
2.3585
2.3739
2.4126
2.4503
2.4787
2.5198
2.5447
2.5950
2.6228
2.6410
2.6812
2.7123
2.7557
2.8584
3.2480
3.5315
3.6808
3.7632
3.7471
3.7283
3.6692
3.6718
3.7756
3.9672
4.0376
3.9092
3.7276
3.6586
3.5948
3.6392
3.5671
3.6003
3.6194
3.6350
3.6624
3.6855
3.6958
3.9105
4.3880
5.1342
5.6176
6.3206
7.0392
7.3767
7.5715
7.6516
7.6469
7.5871
7.4591
7.6004
7.5532
7.3601
7.1487
5.9728
4.8974
4.5850
4.4268
4.3352
4.2887
4.3376
4.3182
4.2909
4.2777
4.2548
4.2677
4.2511
4.2817
4.3847
4.4418
4.4696
4.4932
4.4998
4.5151
4.5096
4.5278
4.5139
4.5020
4.4561
4.4067
4.3841
4.3638
4.3750
4.4366
4.5258
4.6565
4.6485
4.5836
4.5183
4.4583
4.3747
4.3509
4.2938
4.2823
4.2844
4.3135
4.3262
4.3255
4.2568
4.2011
4.1832
4.2278
4.2445
4.2409
4.2784
4.2917
4.3035
4.3015
4.3209
4.3204
4.3356
4.3287
4.3260
4.3483
4.3710
4.3798
4.3802
4.3805
4.5162
4.6906
5.0826
5.6588
6.0137
6.2436
6.5361
7.0790
7.6106
7.6410
7.4120
7.4535
7.2476
7.2596
7.1012
7.0986
6.9395
6.5633
5.8438
4.9434
4.6750
4.4320
4.3063
4.2096
4.0193
3.9698
4.0055
4.0218
4.0426
4.0688
4.0650
3.9793
3.9787
3.9766
3.9981
4.0405
4.0165
4.0290
4.0923
4.0897
4.0615
4.0258
4.0008
4.0274
4.0553
4.0646
4.0442
4.0477
3.9986
4.0354
4.0718
4.0563
4.0189
3.8631
3.8144
3.7736
3.8055
3.9730
4.0299
4.0148
3.8265
3.4675
3.3020
3.2474
3.2338
3.1986
3.1680
3.1289
3.0944
3.0523
3.0094
2.9510
2.9246
2.9057
2.8805
2.8545
2.8245
2.7690
2.7236
2.6833
2.6443
2.5969
2.5415
2.4684
2.4214
2.3699
2.3293
2.2513
2.1963
2.1285
2.0700
2.0209
1.9575
1.8658
1.6996
1.5120
1.4020
1.3087
1.2166
1.1441
1.0774
1.0226
0.9809
0.9448
0.8526
0.6915
0.4491
0.2842
0.2582
0.2570
0.2568
0.2609
0.2632
0.2581
0.2552
0.2539
0.2527
0.2578
0.2672
0.2701
0.2655
0.2658
0.2688
0.2761
0.2767
0.2738
0.2774
0.2801
0.2817
0.2803
0.2830
0.2828
0.2876
0.2952
0.2985
0.3016
0.3092
0.3130
0.3153
0.3182
0.3304
0.3471
0.3416
0.3476
0.3497
0.3453
0.3398
0.3448
0.3563
0.3511
0.3502
0.3481
0.3519
0.3573
0.3544
0.3512
0.3489
0.3499
0.3470
0.3533
0.3409
0.3556
0.3474
0.3435
0.3460
0.3519
0.3447
0.3395
0.3488
0.3473
0.3453
0.3433
0.3484
0.3526
0.3494
0.3607
0.3694
0.4126
0.4604
0.5004
0.5163
0.5328
0.5432
0.5506
0.5485
0.5605
0.5586
0.5622
0.5727
0.5804
0.5797
0.5666
0.5700
0.5696
0.5722
0.5715
0.5656
0.5572
0.5264
0.5156
0.5473
0.6286
0.7503
0.8715
0.8825
0.7507
0.5421
0.2869
0.1091
0.0423
0.0326
0.0343
0.0256
0.0231
0.0281
0.0298
0.0229
0.0283
0.0279
0.0270
0.0300
0.0245
0.0360
0.0280
0.0270
0.0232
0.0276
0.0270
0.0237
0.0197
0.0193
0.0172
0.0140
0.0093
0.0244
0.0226
0.0192
0.0145
0.0124
0.0167
0.0182
0.0111
0.0147
0.0081
0.0151
0.0130
0.0113
0.0131
0.0067
0.0028
0.0064
0.0069
0.0082
0.0075
0.0098
-0.0008
0.0037
0.0019
0.0060
0.0057
0.0033
0.0079
0.0122
0.0091
0.0067
-0.0038
0.0033
0.0013
0.0011
0.0034
0.0051
0.0009
-0.0001
-0.0005
0.0098
-0.0003
0.0067
0.0038
0.0106
0.0000
0.0126
0.0134
0.0090
0.0116
0.0083
0.0101
0.0152
0.0010
0.0068
0.0008
0.0053
0.0090
0.0087
0.0085
0.0054
0.0089
0.0077
0.0064
0.0046
0.0058
0.0025
0.0132
0.0088
0.0043
0.0052
0.0087
0.0122
0.0023
0.0066
0.0093
0.0042
0.0042
0.0138
0.0051
-0.0055
-0.0002
0.0048
0.0063
0.0076
0.0016
-0.0005
0.0086
0.0043
-0.0016
0.0100
0.0097
0.0042
0.0092
0.0051
0.0029
0.0044
0.0033
0.0073
0.0093
0.0077
0.0093
0.0021
0.0026
0.0093
0.0068
0.0039
0.0068
0.0041
0.0053
0.0037
0.0075
0.0016
0.0000
-0.0005
0.0073
0.0076
0.0049
0.0046
0.0087
0.0106
0.0072
0.0085
0.0036
0.0044
0.0043
0.0201
0.0076
0.0075
0.0134
0.0050
0.0071
0.0032
0.0055
0.0085
0.0046
0.0023
-0.0020
0.0027
0.0060
0.0066
0.0067
0.0014
0.0166
0.0067
0.0024
0.0072
0.0062
0.0081
0.0035
0.0077
0.0101
0.0045
0.0034
0.0144
0.0078
0.0065
0.0093
0.0181
0.0028
0.0050
0.0034
0.0063
0.0150
0.0035
0.0022
0.0079
0.0034
0.0110
0.0075
0.0058
0.0085
0.0152
0.0089
0.0060
0.0017
0.0041
0.0091
0.0072
-0.0109
0.0036
0.0063
0.0080
0.0037
0.0086
0.0097
0.0088
0.0016
0.0057
0.0059
0.0139
0.0061
0.0009
0.0059
0.0126
0.0117
0.0003
0.0060
0.0075
0.0073
0.0080
0.0154
0.0136
0.0121
0.0179
0.0150
0.0125
Instead of doing
f_filter = cconv(gf,f,length(f));
this does the trick:
f_filter = conv(gf,f);
f_filter = f_filter(sz/2+1:end-sz/2+1);
As suggested by #AnderBiguri you can use the option 'same' in your convolution fonction to preserve the original size of your array.
But if you apply a convolution with your normalized gaussian filter gf you will obtain a border effect.
To avoid the border effect you can apply the following tricks:
gf = exp(-x .^ 2 / (2 * sigma ^ 2)); %do not normalize gf now
f_filter = conv(f,gf,'same')./conv(ones(length(f),1),gf,'same') %normalization taking into account the lenght of the convolution
For example I've just transformed f into f = f+3
If we do not take into account the border effect we will obtain:

How Solve equation having Matrix parameters in 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

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.

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')