How to find a value among a huge dataset?

2 Ansichten (letzte 30 Tage)
M G
M G am 30 Sep. 2011
Hey guys,
I've got a problem with how to find a value among a series of data in a situation that our value does not necessarily exist in the main dataset. e.g: we have a data array:
data = [1;2;3;4;5;6;7;8;9;10];
and we want to know where is the place for A = 4.365
Of course here it is between data(4,1) and data(5,1) but how can I ask my program to do it in a tremendously huge array? I mean I can do it with loops but is there any special command for that?
Thank you very much :)
Mehdi

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Sep. 2011
Provided that data is strictly increasing:
[junk, binnum] = histc(A, [-inf data inf]);
The binnum returned will be an index K such that A goes strictly before the K'th value -- so if A exactly matches the K'th value, K+1 would be returned. If length(data) + 1 is returned, then the A value either exactly matches or exceeds the last data value.
Feel free to subtract 1 from the result if that makes things easier: that would mean that a returned value of 0 means that the data precedes the first data value.
A can be a vector of values.
Note that, as per usual, if your data values are non-integral, you need to consider round-off errors: a value that is algebraically (say) 4, might numerically get calculated as 4*(1-eps) or even smaller.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by