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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by