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