问题
I am given a table with two columns - one is P_in and the other P_out:
P_in = table(:,1);
P_out = table(:,2);
I am also given a subset of P_in, which I call P_in2. I want to find the corresponding value of P_out that belongs to P_in2.
I tried find():
P_out2_idx = find(table(P_in2,2));
P_out2 = table(P_out2,idx,2);
But find() can only deal with integers. My table however, does not show integers, but decimals. Here are three lines from the table:
-13.02 49.6
-12.52 49.9
-12.02 50.18
So even rounding the P_in2 values wouldn't work.
回答1:
Using ismember is good for finding multiple indices if your numbers are integers. For floating-point numbers a safer way is using ismembertol:
isInP_in2 = ismembertol(P_in, P_in2, eps);
P_out2 = P_out(isInP_in2);
The returned array isInP_in2 is a logical array the size of P_in, which is true at the indices where values in P_in2 are found, and false otherwise.
The third argument is the tolerance used for comparisons (here we take the floating-point relative accuracy eps). For example, searching for a single index using find would be done:
idx = find(abs(P_in - P_in2(1)) < eps);
来源:https://stackoverflow.com/questions/41609994/how-to-use-find-in-matlab-in-order-to-find-decimals