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 Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by