Your problem is that the find(pH_fine == pH_labvals(i))
on the right side sometimes doesn't find any match, and returns an empty result for an index, specifically a 1-by-0 row vector. This doesn't match the size of the left side, which is indexing a 1-by-1 element from your vector index_labvals
.
You need to check first if the result of find
is empty, and decide what you will put in the index vector in that case, like a 0 or NaN
. You will also need to deal with find
giving you a vector of indices if pH_labvals
has the same value repeated. If you simply want to remove repeated values, you could use unique
like so:
pH_labvals = unique(pH_labvals, 'stable');
If you're wondering why you're getting an empty result from find
, you should read through this post about the perils of floating-point comparison. One possible solution, assuming pH_labvals
contains non-repeated values with 2 decimal places of precision, is to first round your pH_fine
vector to 2 decimal places:
pH_fine = round(pH(1):0.01:pH(end), 2);
This should allow you to avoid the errors from floating-point comparison.