torch.searchsorted#
- torch.searchsorted(sorted_sequence,values,*,out_int32=False,right=False,side=None,out=None,sorter=None)→Tensor#
Find the indices from theinnermost dimension of
sorted_sequencesuch that, if thecorresponding values invalueswere inserted before the indices, when sorted, the orderof the correspondinginnermost dimension withinsorted_sequencewould be preserved.Return a new tensor with the same size asvalues. More formally,the returned index satisfies the following rules:sorted_sequencerightreturned index satisfies
1-D
False
sorted_sequence[i-1]<values[m][n]...[l][x]<=sorted_sequence[i]1-D
True
sorted_sequence[i-1]<=values[m][n]...[l][x]<sorted_sequence[i]N-D
False
sorted_sequence[m][n]...[l][i-1]<values[m][n]...[l][x]<=sorted_sequence[m][n]...[l][i]N-D
True
sorted_sequence[m][n]...[l][i-1]<=values[m][n]...[l][x]<sorted_sequence[m][n]...[l][i]- Parameters
- Keyword Arguments
out_int32 (bool,optional) – indicate the output data type. torch.int32 if True, torch.int64 otherwise.Default value is False, i.e. default output data type is torch.int64.
right (bool,optional) – if False, return the first suitable location that is found. If True, return thelast such index. If no suitable index found, return 0 for non-numerical value(eg. nan, inf) or the size ofinnermost dimension within
sorted_sequence(one pass the last index of theinnermost dimension). In other words, if False,gets the lower bound index for each value invalueson the correspondinginnermost dimension of thesorted_sequence. If True, gets the upperbound index instead. Default value is False.sidedoes the same and ispreferred. It will error ifsideis set to “left” while this is True.side (str,optional) – the same as
rightbut preferred. “left” corresponds to False forrightand “right” corresponds to True forright. It will error if this is set to“left” whilerightis True. Default value is None.out (Tensor,optional) – the output tensor, must be the same size as
valuesif provided.sorter (LongTensor,optional) – if provided, a tensor matching the shape of the unsorted
sorted_sequencecontaining a sequence of indices that sort it in theascending order on the innermost dimension
Example:
>>>sorted_sequence=torch.tensor([[1,3,5,7,9],[2,4,6,8,10]])>>>sorted_sequencetensor([[ 1, 3, 5, 7, 9], [ 2, 4, 6, 8, 10]])>>>values=torch.tensor([[3,6,9],[3,6,9]])>>>valuestensor([[3, 6, 9], [3, 6, 9]])>>>torch.searchsorted(sorted_sequence,values)tensor([[1, 3, 4], [1, 2, 4]])>>>torch.searchsorted(sorted_sequence,values,side='right')tensor([[2, 3, 5], [1, 3, 4]])>>>sorted_sequence_1d=torch.tensor([1,3,5,7,9])>>>sorted_sequence_1dtensor([1, 3, 5, 7, 9])>>>torch.searchsorted(sorted_sequence_1d,values)tensor([[1, 3, 4], [1, 3, 4]])