Bug in matrix indexing?

조회 수: 1 (최근 30일)
Stewart Tan
Stewart Tan 2019년 9월 4일
편집: Stephen23 2023년 4월 20일
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
Stephen23
Stephen23 2019년 9월 4일
편집: Stephen23 2023년 4월 20일
"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
Stewart Tan 2019년 9월 4일
@Stephen Cobeldick thanks, i have overlooked that display format.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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
Stewart Tan
Stewart Tan 2019년 9월 4일
Is Matlab’s default short?
Walter Roberson
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
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));

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by