Why does inequality give different values even though its evaluating the exact same matrix ??

3 ビュー (過去 30 日間)
Howcome these two yield different values ??
load td
version1 = sort(td(td(:,5) < 11.29))
version1 = 8×1
11.1700 11.1750 11.1750 11.1901 11.2000 11.2200 11.2848 11.3312
t = (td(:,5));
version2 = sort(t(t < 11.29))
version2 = 8×1
11.1700 11.1750 11.1750 11.2000 11.2200 11.2486 11.2800 11.2848
version1 - version2
ans = 8×1
0 0 0 -0.0099 -0.0200 -0.0286 0.0048 0.0464

採用された回答

Walter Roberson
Walter Roberson 2021 年 12 月 11 日
sort(td(td(:,5) < 11.29))
td is a 2D array with multiple columns. You construct a mask from one of the columns, and you use it to index the entire td array -- which by linear indexing is going to effectively have it operate on the first column.
t = (td(:,5));
sort(t(t < 11.29))
but here you extract the column and your linear indexing is against the extracted version that only contains the one column.
  2 件のコメント
HabenG
HabenG 2021 年 12 月 11 日
Thanks that explains it. I did that to avoid the binary out put from the inequality. what would be a good way to evalueate the inequality and get the actual values?
Walter Roberson
Walter Roberson 2021 年 12 月 11 日
version3 = sort(td(td(:,5) < 11.29, 5))
is a possible way. But your second version with a temporary variable works well and is easier to read.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by