On Apr 22, 12:41 pm, "Alistair Templeton" <bigalt2...@yahoo.com>
wrote:
> here's a nonrobust, obnoxious way to play with:
>
> key(find(diff(key>=newkey))+1)

Not sure I buy it Alistair. It's not working for me, unless I totally
misunderstood what he said. See my demo, with code that does what I
believe he said:
% Generate sample data.
key = [1 0 1 2 3 3 4 4 6 7] % Bruce says this will be sorted
already.
% Find the index in KEY whose value is the same as
% (easy) or the one closest to but less than NEWKEY
newkey = 3.8
% There is no value the same as 3.8 in key,
% Therefore we look for the closest value
% that is less than 3.8.
% This would be a value of 3 and it occurs
% at indexes 5 and 6.
diffs = newkey  key
% Find out where the last place is that we have a value
% less than newkey.
highestIndex = find(diffs >= 0, 1, 'last') % this will find index of
6
% Find the value of key at that location.
valueThere = key(highestIndex)
% Now find any more elements that also have that value.
allMatchingIndices = find(key == valueThere)
% Alistair's solution:
AlistairSolution = key(find(diff(key>=newkey))+1)
% Hmmmm.....didn't seem to find the right
% indexes or values.
% Now try again, this time trying to find a value
% that exists in the array: 4.
% Find the index in KEY whose value is the same as
% (easy) or the one closest to but less than NEWKEY
newkey = 4
% There is a value of 4 in key,
% It occurs at indexes 7 and 8.
diffs = newkey  key
% Find out where the last place is that we have a value
% less than newkey.
highestIndex = find(diffs >= 0, 1, 'last') % this will find index of
6
% Find the value of key at that location.
valueThere = key(highestIndex)
% Now find any more elements that also have that value.
allMatchingIndices = find(key == valueThere)
% Alistair's solution:
AlistairIndexes = find(diff(key>=newkey))+1
AlistairSolution = key(find(diff(key>=newkey))+1)
% Hmmmm.....found one correctly, but didn't find
% the other one of the indexes.
Try to find the 3s:
key =
1 0 1 2 3 3 4 4 6 7
newkey =
3.8000
diffs =
4.8000 3.8000 2.8000 1.8000 0.8000 0.8000
0.2000 0.2000 2.2000 3.2000
highestIndex =
6
valueThere =
3
allMatchingIndices =
5 6
AlistairSolution =
4
Now trying to find the 4s:
newkey =
4
diffs =
5 4 3 2 1 1 0 0 2 3
highestIndex =
8
valueThere =
4
allMatchingIndices =
7 8
AlistairIndexes =
7
AlistairSolution =
4
