How do I refer to values that contain decimal points and integers (for example, 1.0000 ... and 1)?

1 回表示 (過去 30 日間)
I want to take out the row in which the first column of the cell array b (above image) is 1 (including 1.0000) and put it into the cell array a.
When I wrote the code like this, I can only get the integer 1.
a = b(cellfun(@(x) x == 1, b(:,1)),:);
How do I get 1.0000 rows too?
  1 件のコメント
Stephen23
Stephen23 2021 年 9 月 22 日
編集済み: Stephen23 2021 年 9 月 22 日
"I want to take out the row in which the first column of the cell array b (above image) is 1 (including 1.0000)"
Your code matches values exactly equal to one (as indicated by "1").
But "1.0000" indicates a value that it is not exactly one.
If you want to include vaues that are not quite equal to one (such as those indicated by "1.0000") then you will have to include a tolerance in your value comparison. You will have to decide how much tolerance is acceptable.
A more efficient approach than CELLFUN:
tol = 1e-10;
idx = [b{:,1}] < tol;
out = b(idx,;)

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

採用された回答

Chunru
Chunru 2021 年 9 月 22 日
a = b(cellfun(@(x) abs(x -1 ) <1e-10, b(:,1)),:);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by