Problems when creating matrix

조회 수: 3 (최근 30일)
Juan Manuel Romero
Juan Manuel Romero 2019년 10월 21일
편집: Stephen23 2019년 10월 22일
Hello,
I am trying to create a matrix by several means, using linespace, using matrix notation and stating every element. For some reason the results obtained are not equal for some of the values in the matrix:
categ1=linspace(0.85,1.35, 51)
categ2=[0.85:0.01:1.35]
categ3=[0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.2 1.21 1.22 1.23 1.24 1.25 1.26 1.27 1.28 1.29 1.3 1.31 1.32 1.33 1.34 1.35]
categ1==categ2
categ2==categ3

답변 (1개)

Daniel M
Daniel M 2019년 10월 21일
The results are equal up to double precision. Look at this:
max(abs(categ1-categ2))
% ans =
% 2.22044604925031e-16
eps
% ans =
% 2.22044604925031e-16
So the values are equal up to the limit that the computer can differentiate between them.
It seems that linspace and colon have slightly different implementations. If you need something with higher precision, then look into John D'Errico's VPI toolbox.
  댓글 수: 2
Juan Manuel Romero
Juan Manuel Romero 2019년 10월 22일
The problem is that when I create the matrix whit linspace and colon and later try to find the colation of the value equal to 1.2 get an empty matrix. Any idea of how can I solve that issue?
>> categ1=linspace(0.85,1.35, 51);
categ2=[0.85:0.01:1.35];
find(categ1==1.2)
find(categ2==1.2)
ans =
1×0 empty double row vector
ans =
1×0 empty double row vector
Stephen23
Stephen23 2019년 10월 22일
편집: Stephen23 2019년 10월 22일

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

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by