r元素的矩阵个体移位运算 [英] r matrix individual shift operations of elements

查看:96
本文介绍了r元素的矩阵个体移位运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试优化我编写的某些代码,因为这对于大型数据集而言非常慢.我不确定是否可以通过矩阵运算来完成以下操作,如果有人有任何建议可以加快运算速度,我将不胜感激.

I am trying to optimize some code that I have written as it is very slow for large datasets. I am not sure if the following can be done with matrix operations and I would appreciate if someone had any suggestions to make it faster.

我有一个包含零和整数的矩阵,我想将各个列的条目向下移动条目中整数的绝对值.

I have a matrix with zeros and integers and I would like to shift down the entries of the individual columns by the absolute number of the integer in the the entry.

   [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0   -4    0
[3,]    4    0    0
[4,]   -3   -2    0
[5,]    0    2   -1
[6,]    2   -2    0
[7,]    0    0    0
[8,]   -3   -3    0  

我正在使用的代码如下:

The code I am using is the following:

#data
A<-matrix(data=c(0,0,4,-3,0,2,0,-3,0,-4,0,-2,2,-2,0,-3,0,0,0,0,-1,0,0,0),nrow=8,ncol=3)

#shift function
shift<-function(x)
{
  #create the output matrix
  out<-matrix(data=0,nrow=8,ncol=1)

  #for loop to create the shift matrix
  for(i in seq(1,8,by=1))
  {
    if(i+abs(x[i])<=8)
    {
      #find the non zero
      if(x[i]!=0)
      {
        #if there is already a number put zero  
        if(out[i+abs(x[i]),1]!=0)
        {
          out[i+abs(x[i]),1]=0
        } else {
          #shift
          out[i+abs(x[i]),1]=x[i]
        }
      }
    }
  }

  #return object
  return(out)
}

#run the logic
shift_mat<-sapply(1:ncol(A),FUN=function(k) shift(A[,k]))

结果是:

   [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    0    0    0
[5,]    0    0    0
[6,]    0    0   -1
[7,]    0    2    0
[8,]    2   -2    0

每列的规则如下:

  1. 从顶部开始查找与第一个条目不同的第一个条目 零
  2. 向下移动该条目的绝对数字
  3. 如果在目标点有另一个条目,则置零
  4. 重复下一页
  1. starting from the top find first entry that is different than zero
  2. shift down by the absolute numbers of that entry
  3. if there is another entry at the targeted point put zero
  4. repeat for the next column

谢谢

尼科斯

推荐答案

在我的计算机上使用您的示例,此方法更干净,速度提高了约40%.也许使用更大的数据可以提高速度?

This is a bit cleaner and about 40% faster using your example on my machine. Maybe the speed improvement will be greater using your larger data?

您应该使用整数矩阵.它使用更少的内存,并且某些操作更快:

You should use a matrix of integers. It uses less memory and some operations are faster:

A <- matrix(as.integer(c(0,0,4,-3,0,2,0,-3,0,-4,0,-2,2,
                        -2,0,-3,0,0,0,0,-1,0,0,0)), nrow = 8, ncol = 3)

每列都是一个向量,因此您的输出也应该是.我用向量代替了矩阵.还使您的代码更加健壮,而无需进行硬编码的行数:

Each column is a vector, so should be your output. I replaced matrices with vectors. Also made your code more robust without the hardcoded number of rows:

shift <- function(x) {
  n <- length(x)
  y <- rep(0L, n)
  for(i in seq_len(n)) {
    if (x[i] == 0L) next
    j <- i + abs(x[i])
    if (j > n) next
    y[j] <- if (y[j] != 0L) 0L else x[i]
  }
  return(y)
}

您可以使用apply运行它:

shift_mat <- apply(A, 2, shift)

这篇关于r元素的矩阵个体移位运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆