Bug in matrix indexing?
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
"Why is it that only the first part is extracted correctly from the matrix?"
It isn't, all of them are extracted correctly.
Pay attention to the display format of your original matrix, which starts with this:
1.0e+03 *
...
Stewart Tan
on 4 Sep 2019
Accepted Answer
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));
Categories
Find more on Get Started with MATLAB 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!