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

2 Ansichten (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 22 Sep. 2021
Bearbeitet: Stephen23 am 22 Sep. 2021
"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,;)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Chunru
Chunru am 22 Sep. 2021
a = b(cellfun(@(x) abs(x -1 ) <1e-10, b(:,1)),:);

Weitere Antworten (0)

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by