由买买提看人间百态

topics

全部话题 - 话题: colsums
(共0页)
M**********s
发帖数: 8
1
来自主题: JobHunting版 - twitter 一题

注意
1.Up[0]+...Up[i-1] == Down[i+1]+...Down[n-1]只与i相关
2.Left[0]+..Left[j-1] == Left[j+1]...Left[m-1]只与j相关
故1式成立时,称i行为平衡行
当2式成立时,称j行为平衡列
行列的关系彼此独立,故平衡数总个数=平衡行数*平衡列数
剩下就是一些实作细节问题
其实只要记住每行每列和,在迴圈中就可判断是否为平衡行列了
下面是O(m+n) space, O(m*n) time的实作
其实还满简单的,只是稍长了些
int balancingCells(vector >& matrix) {
if (matrix.empty()) return 0;
int nRow = matrix.size(), nCol = matrix[0].size();
vector rowSum(nRow, 0), colSum(nCol, 0);
int total = 0;
for (int r=0; r for (int c=... 阅读全帖
T****s
发帖数: 915
2
来自主题: JobHunting版 - 请教Leetcode 上的 Sudoku solver
用python 实现,可是不知道问题出在哪里,结果显示output 和 input 一模一样。
请教大家。
谢谢。
~~~~~~~~~~~~~~~~~
class Solution:
# @param board, a 9x9 2D array
# Solve the Sudoku by modifying the input board in-place.
# Do not return any value.
class _State:
def __init__(self, board, row, col, rowsum, colsum, blocksum):
# board --- the board at the current state
# whichRow and whichCol --- row and col indices that need to
work
on (unfilled)
# rowStat, colStat and blockStat... 阅读全帖
s*****n
发帖数: 2174
3
来自主题: Statistics版 - 今天又“R”了 -- 感想和请教。
lapply(split(data[, 1:2], list(data[,3], data[,4])), colSums)
就可以了.
上面有人提到了by( ), 也不错, 非常方便.
我以前还真不知道.
不过如果你希望继续应用这个东西的结果的话,
建议用lapply, 因为它的输出是list, 很容易后续操作.
还有就是lapply+split的执行效率要比tapply稍高一些.
> system.time( for (i in 1:2000) by(data[,1:2],data[,3:4],colSums))
[1] 6.58 0.01 6.05 NA NA
> system.time( for (i in 1:2000) lapply(split(data[, 1:2], list(data[,3],
data[,4])), colSums))
[1] 5.52 0.00 5.20 NA NA
s*****n
发帖数: 2174
4
来自主题: Statistics版 - 今天又“R”了 -- 感想和请教。
哦, 看明白你最后那个问题了, 你可能是希望这样
data <- data.frame(
a=c(1,2,1,2),
b=c(2,3,4,5),
c=c(3,3,4,4))
根据c的不同取值, 同时对a和b应用某函数, 比如求和, 输出是这样的
a b (c=3)
3 5
a b (c=4)
3 9
如果是这样的话, tapply是不行的, 因为tapply的第一参数只能是vector, 不能是矩阵
. 不过你可以人为实现, 先用split函数把data frame分解成小块成为list, 然后应用
lapply和colSums函数.
> lapply(split(data[, 1:2], data[,3]), colSums)
$`3`
a b
3 5
$`4`
a b
3 9
实际上, tapply的优势在于多维度的计算, 而lapply和split更清晰灵活., 速度上更快
.
l*******l
发帖数: 204
5
来自主题: Statistics版 - 今天又“R”了 -- 感想和请教。
Please try first and then come here to ask. May you are trying to test songkun (告别棒球场).
lapply(split(data[, 1:2], data[,3:4]), colSums)
Here is another option.
by(data[,1:2],data[,3:4],colSums)
r********e
发帖数: 33
6
Where to get free C++ library for matrix computation?
I need to do matrix multiplication, inverse, trace, diagonal, rowSum,
colSum etc, eigen value/vector, Hessian, SVD, etc..
Are they available for public for free?
Thanks!
Happy New Year!
c*2
发帖数: 24
7
colSum(A)/nrow(A)
This is faster for large matrix.
c*2
发帖数: 24
8
Sorry, should be colSums/nrow(A)
t*****w
发帖数: 254
9
来自主题: Statistics版 - 请问面试 R 应该怎么准备?
When I had my job interview, they always tested my SAS skill.However I use R
all the time. To help your preparation, read my R codes to see how much you
can understand it.
%in%
?keyword
a<-matrix(0,nrow=3,ncol=3,byrow=T)
a1 <- a1/(t(a1)%*%spooled%*%a1)^.5 #standadization in discrim
a1<- a>=2; a[a1]
abline(h = -1:5, v = -2:3, col = "lightgray", lty=3)
abline(h=0, v=0, col = "gray60")
abs(r2[i])>r0
aggregate(iris[,1:4], list(iris$Species), mean)
AND: &; OR: |; NOT: !
anova(lm(data1[,3]~data1[,1... 阅读全帖
(共0页)