How to use find() to find the corresponding output (not index!) of an input value

7 views (last 30 days)
I am given a table with 2 columns - one contains P_in, the other P_out:
P_in = table(:,1)
P_out = table(:,2)
I want MATLAB to find the corresponding values of P_out to a set of P_in. I.e. I have P_in2 which is a subset of P_in. I want MATLAB to return the corresponding output values. I tried:
P_in2 = find(table(P_in2,2));
I get an error: Subscript indices must either be real positive integers or logicals.
So apparently find() can only deal with integers. Also, I think, find() only returns indices. How can I solve this problem?

Accepted Answer

Jyotish Robin
Jyotish Robin on 16 Jan 2017
Hi Luki,
You could try using the functions "arrayfun" and "find" together to resolve the issue.
The relevant indices can be found using the following command:
>>idxs = arrayfun(@(x)find(P_in==x,1),P_in2)
Now you can use these indices to index into P_out as follows:
>>ans = P_out(idxs);
Hope this helps!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!