按行删除矩阵

请教一下, 矩阵有没有 删除行的功能? 我看 drop 可以删除列。

请先 登录 后评论

2 个回答

veryOrdinary
m = 1..10000000 $ 1000000:10
n = -200
// 可以参考以下几种实现,其中 4 性能最高
timer{index = til(m.rows()).drop(n); m[index,]}
timer iif(n>0, m[n:,], m[:(m.rows()+n),])
timer m.transpose().drop(n).transpose()
timer eachLeft(def(x,y): x.drop(y), m, n);
请先 登录 后评论
SaintM

测试脚本

// 生成一个10列,100万行的矩阵
cols = 10
rows = 1000000
m = 1..(cols*rows)$rows:cols
// 删去的行数为从结尾开始的200行
n = -2
// 方案一: slice by index
timer{ index = til(m.rows()).drop(n); m[index,]}
// 方案二:iif
timer iif(n>0, m[n:,], m[:(m.rows()+n),])
// 方案三: transpose 两次 + drop
timer m.transpose().drop(n).transpose()
// 方案四:eachLeft + drop
timer eachLeft(def(x,y): x.drop(y), m, n);

测试环境

本地windows机器,2.00.11 dolphindb,workerNum=4,maxMemSize=32

测试数据
耗时统计方法 timer 统计计算执行的耗时

  1. 方案一,耗时(ms):15.03
  2. 方案二,耗时(ms):36
  3. 方案三,耗时(ms):138.04
  4. 方案四,耗时(ms):11.03
请先 登录 后评论