Bug in matrix indexing?
3 次查看(过去 30 天)
显示 更早的评论
So i have the matrix below, which are the DCT coefficients for an image.
test =
1.0e+03 *
1.1506 -0.0094 -0.0043 -0.0012 -0.0001 -0.0007 0.0003 0.0004
-0.0082 0.0004 -0.0012 0.0007 0.0017 -0.0003 -0.0011 0.0005
0.0008 -0.0002 -0.0015 0.0001 0.0004 0.0010 -0.0002 0.0014
0.0002 -0.0000 -0.0018 0.0010 -0.0018 -0.0005 0.0009 -0.0012
0.0001 0.0001 -0.0024 -0.0008 0.0004 0.0010 0.0011 -0.0001
-0.0012 0.0004 -0.0001 -0.0007 -0.0004 -0.0002 -0.0001 0.0009
0.0003 0.0002 0.0011 -0.0002 -0.0014 -0.0004 0.0000 -0.0004
0.0005 -0.0002 -0.0021 -0.0001 0.0007 0.0005 0.0008 -0.0007
and I'm splitting the matrix above into 4 equal parts since it's an 8x8 matrix. The first part would contain
1.1506 -0.0094 -0.0043 -0.0012
-0.0082 0.0004 -0.0012 0.0007
0.0008 -0.0002 -0.0015 0.0001
0.0002 -0.0000 -0.0018 0.0010
second part would be
-0.0001 -0.0007 0.0003 0.0004
0.0017 -0.0003 -0.0011 0.0005
0.0004 0.0010 -0.0002 0.0014
-0.0018 -0.0005 0.0009 -0.0012
etc..
So i managed to index out the first part, with.
N = 8;
test(1:N/2, 1:N/2)
which gave me the first part above. But suddenly, when i want to get the second part and the rest, I'm getting weird outputs as shown.
sec_part = test(1:N/2, N/2+1:N) %2nd part
sec_part =
-0.1250 -0.7400 0.3082 0.3531
1.6533 -0.3160 -1.0713 0.5453
0.3779 0.9798 -0.1831 1.3921
-1.8311 -0.4653 0.8852 -1.2236
I was supposed to get:
-0.0001 -0.0007 0.0003 0.0004
0.0017 -0.0003 -0.0011 0.0005
0.0004 0.0010 -0.0002 0.0014
-0.0018 -0.0005 0.0009 -0.0012
but I'm getting the random numbers as shown in sec_part. The same happened for the third part and fourth part. Why is it that only the first part is extracted correctly from the matrix?
2 个评论
采纳的回答
Walter Roberson
2019-9-4
You missed part of the output. Notice you have
test =
1.0e+03 *
That 1.0e+03 means that the part displayed below all has to be multiplied by 1000 to get the actual numbers. So for the -0.0001 entry displayed, the real value stored is roughly -0.1 -- with -0.1250 being entirely consistent with that possibility.
I recommend that you stop using "format short" and start using "format short g" or "format long g"
There is a Preference you can set to change the default output format.
2 个评论
Walter Roberson
2019-9-4
Yes, the default is short.
Preferences -> Command Window -> Text Display -> Numeric format
This preference controls what you get when you start up MATLAB; you can change the active format at any time using a format command.
更多回答(1 个)
KSSV
2019-9-4
USe this:
test = 1.0e+03 *[1.1506 -0.0094 -0.0043 -0.0012 -0.0001 -0.0007 0.0003 0.0004
-0.0082 0.0004 -0.0012 0.0007 0.0017 -0.0003 -0.0011 0.0005
0.0008 -0.0002 -0.0015 0.0001 0.0004 0.0010 -0.0002 0.0014
0.0002 -0.0000 -0.0018 0.0010 -0.0018 -0.0005 0.0009 -0.0012
0.0001 0.0001 -0.0024 -0.0008 0.0004 0.0010 0.0011 -0.0001
-0.0012 0.0004 -0.0001 -0.0007 -0.0004 -0.0002 -0.0001 0.0009
0.0003 0.0002 0.0011 -0.0002 -0.0014 -0.0004 0.0000 -0.0004
0.0005 -0.0002 -0.0021 -0.0001 0.0007 0.0005 0.0008 -0.0007] ;
[m,n] = size(test);
k = [4 4]; % you want 4*4 matrix
C = mat2cell(test,k(1)*ones(m/k(1),1),k(2)*ones(n/k(2),1));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!