Bug in matrix indexing?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Walter Roberson
on 4 Sep 2019
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 Comments
Walter Roberson
on 4 Sep 2019
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.
More Answers (1)
KSSV
on 4 Sep 2019
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 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!