我希望得到一个向量的某个子集的最小值索引,但是得到原始向量中的索引,而不是重新编号的子集。
至于现在,我一直在使用:
L = rnorm(20) # say this is the original vector
subset = runif(20)<0.3 # some conditions to extract the subset
ind_min = which.min(L[subset])
ind_sel = seq(L)[subset]
ind_min = ind_sel[ind_min]
但我想应该有更直接或更清洁的东西。我一直在考虑使用诸如此类的技巧:
L_tmp = L
L_tmp[!subset] = Inf
ind_min = which.min(L_tmp)
显然效率更高:
> microbenchmark(method_1(), method_2(), unit = "relative")
Unit: relative
expr min lq mean median uq max neval
method_1() 3.699562 3.249635 3.119666 3.076819 2.928259 3.225849 100
method_2() 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 100
但我对此并不满意,因为我猜应该还有别的东西。有什么建议吗?
答案 0 :(得分:5)
你可以尝试:
(seq(L))[subset][which.min(L[subset])]
类似于您的第一个方法,但没有创建临时变量
20000长矢量L
上的基准测试结果:
method_cath<- function(){(seq(L))[subset][which.min(L[subset])]}
method_FK_corr1 <- function(){min = min(L[subset])
ind_min = intersect(which(L == min), seq(L)[subset])[1]
return(ind_min)}
method_FK_corr2 <- function(){min = min(L[subset])
ind_min = intersect(which(L == min), which(subset))[1]
return(ind_min)}
method_1clm <- function(){ind_min = which.min(L[subset])
ind_sel = seq(L)[subset]
ind_min = ind_sel[ind_min]
return(ind_min)}
method_2clm <- function(){L_tmp = L
L_tmp[!subset] = Inf
ind_min = which.min(L_tmp)
return(ind_min)}
> microbenchmark(method_2clm(), method_cath(), method_1clm(), method_FK_corr2(), method_FK_corr1(), unit = "relative")
# Unit: relative
# expr min lq mean median uq max neval cld
# method_2clm() 1.000000 1.000000 1.000000 1.000000 1.000000 1.0000000 100 a
# method_cath() 1.312146 1.290370 1.282964 1.278178 1.282424 0.9191693 100 b
# method_1clm() 1.295031 1.294642 1.303781 1.284630 1.279821 1.2977193 100 b
# method_FK_corr2() 1.185821 1.166924 1.278030 1.155217 1.165738 4.9948007 100 b
# method_FK_corr1() 1.683783 1.644797 1.746055 1.635293 1.636195 5.1616672 100 c
注意:我使用@FedorenkoKristina原始函数得到NA
,我测试了2个可能的校正函数,现在所有函数都给出了相同的结果。
答案 1 :(得分:2)
您可以在<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dip">
<info.androidhive.slidingmenu.util.TouchImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name" />
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
找到分钟。然后在L中获得索引。
L[subset]
答案 2 :(得分:0)
您也可以使用子集。子集是你的条件。
L = rnorm(20) # say this is the original vector
subset = runif(20)<0.3 # some conditions to extract the subset
ind_min = which(L == min(subset(L,subset)))
我猜这与Fedorenko Kristina建议的非常相似。她比我快。