f*****u 发帖数: 17 | 1 应该是比较简单的问题,求助。
1. 数列a_1,...a_n. 如何不用循环求 sum( a_j: a_j>K).K 为常数。
2. 如何求 sum_{1<=j<=n} {max(a_j,K)} 。
应该是比较简单的。谢谢· | d**n 发帖数: 198 | 2 homework?
【在 f*****u 的大作中提到】 : 应该是比较简单的问题,求助。 : 1. 数列a_1,...a_n. 如何不用循环求 sum( a_j: a_j>K).K 为常数。 : 2. 如何求 sum_{1<=j<=n} {max(a_j,K)} 。 : 应该是比较简单的。谢谢·
| t**i 发帖数: 688 | 3 这些本质上都是循环,不然无解。当然你可以用现成的函数,看起来至少没有外在的
for,while循环而已。
sum(vec[vec>K]) | s*r 发帖数: 2757 | 4 multiple it with a vector with 0 and 1
【在 f*****u 的大作中提到】 : 应该是比较简单的问题,求助。 : 1. 数列a_1,...a_n. 如何不用循环求 sum( a_j: a_j>K).K 为常数。 : 2. 如何求 sum_{1<=j<=n} {max(a_j,K)} 。 : 应该是比较简单的。谢谢·
| s*****n 发帖数: 2174 | 5 a <- c(3,6,2,5,7,9)
K <- 4
## Question 1
sum(a[a > K])
## Question 2
sum(apply(rbind(K, a), 2, max)) | q**j 发帖数: 10612 | 6 请教一下啊,
> sum(a[a<=K]<-K)
为什么这个不行? | b*******g 发帖数: 513 | 7 ##Question 1:
sum(t(a)%*%(a>k))
##Question 2:
sum(t(a)%*%(a>K))+sum(t(rep(K))%*%(a
【在 s*r 的大作中提到】 : multiple it with a vector with 0 and 1
| q**j 发帖数: 10612 | 8 问题1:a[a<=K]<-K evalaute以后的结果为什么是4?
问题2:怎么样告诉R我要的是a,而不是a[a<=K]<-K evaluate 以后的结果?
【在 q**j 的大作中提到】 : 请教一下啊, : > sum(a[a<=K]<-K) : 为什么这个不行?
| q**j 发帖数: 10612 | | s*****n 发帖数: 2174 | 10 sum(a[a <= K] <- K)
里面包含了下面几个步骤
1. subscripting a[a <= K]
2. repeat K to the same length of a[a <= K], rep(K, length=sum(a <= K))
3. assign value of 2 to 1.
4. take a sum.
最后一步sum()的参数的是K, 不是a[a <= K], 更不是a了.
【在 q**j 的大作中提到】 : 请教一下啊, : > sum(a[a<=K]<-K) : 为什么这个不行?
| | | s*****n 发帖数: 2174 | 11 第二个也可以, 不过不推荐这样做, 有点ambiguous.
做矩阵乘法, R会自动根据情况决定是使用行向量还是列向量.
但是如果两个程子都是向量. 那么R会默认用内积的形式乘, 而不是外积的形式.
可以试试:
a <- c(1, 2)
b <- c(3, 4)
t(a) %*% b ## 内积形式
a %*% t(b) ## 外积形式
a %*% b ## ambigious, 但是默认用内积形式
【在 q**j 的大作中提到】 : 为什么第一个里面可以去掉t(),第二个不行?
| q**j 发帖数: 10612 | 12 it is still not clear to me at step 4, how do you know the parameter of sum(
) is K?
following your argument K is reped twice in this example, at least the resul
t should be 8, right?
【在 s*****n 的大作中提到】 : sum(a[a <= K] <- K) : 里面包含了下面几个步骤 : 1. subscripting a[a <= K] : 2. repeat K to the same length of a[a <= K], rep(K, length=sum(a <= K)) : 3. assign value of 2 to 1. : 4. take a sum. : 最后一步sum()的参数的是K, 不是a[a <= K], 更不是a了.
| s*****n 发帖数: 2174 | 13 no, it is equivalent to sum(K), where K is a scaler in my example.
technically, R consider sum(a <- b) as two independent jobs:
sum(b) and a <- b, instead of a sequence of jobs a <- b and then sum(a).
In my opinion, this kind of expression should be completely
prohibited, since it is ambiguous.
sum(
resul
【在 q**j 的大作中提到】 : it is still not clear to me at step 4, how do you know the parameter of sum( : ) is K? : following your argument K is reped twice in this example, at least the resul : t should be 8, right?
| s*****n 发帖数: 2174 | 14 You are right, it is NOT obvious.
This is why you should not do this kind of thing.
Do not try to condense the codes.
sum() take an object as the input and calculate the sum of this object. But,
when you write sum(a <- b), what is your object? It is unclear if you want
to do sum(a) or sum(b) or sum(the object passed by "<-"). R, by default,
uses the first evaluable item.
rule?
be
【在 q**j 的大作中提到】 : it is still not clear to me at step 4, how do you know the parameter of sum( : ) is K? : following your argument K is reped twice in this example, at least the resul : t should be 8, right?
| q**j 发帖数: 10612 | 15 which place in R manual can I find information where this behavioral is defi
ned? i kind of want to know more about it.
【在 s*****n 的大作中提到】 : no, it is equivalent to sum(K), where K is a scaler in my example. : technically, R consider sum(a <- b) as two independent jobs: : sum(b) and a <- b, instead of a sequence of jobs a <- b and then sum(a). : In my opinion, this kind of expression should be completely : prohibited, since it is ambiguous. : : sum( : resul
| q**j 发帖数: 10612 | 16 thanks a lot for the help!
But,
【在 s*****n 的大作中提到】 : You are right, it is NOT obvious. : This is why you should not do this kind of thing. : Do not try to condense the codes. : sum() take an object as the input and calculate the sum of this object. But, : when you write sum(a <- b), what is your object? It is unclear if you want : to do sum(a) or sum(b) or sum(the object passed by "<-"). R, by default, : uses the first evaluable item. : : rule? : be
| s*****n 发帖数: 2174 | 17 read the
4.3.3 Argument evaluation
in the <>
defi
【在 q**j 的大作中提到】 : which place in R manual can I find information where this behavioral is defi : ned? i kind of want to know more about it.
| q**j 发帖数: 10612 | 18 i see what you are saying. but the second one does not work without t(). you
will see if you try it.
【在 s*****n 的大作中提到】 : 第二个也可以, 不过不推荐这样做, 有点ambiguous. : 做矩阵乘法, R会自动根据情况决定是使用行向量还是列向量. : 但是如果两个程子都是向量. 那么R会默认用内积的形式乘, 而不是外积的形式. : 可以试试: : a <- c(1, 2) : b <- c(3, 4) : t(a) %*% b ## 内积形式 : a %*% t(b) ## 外积形式 : a %*% b ## ambigious, 但是默认用内积形式
| s*****n 发帖数: 2174 | 19 it works on my machine
> a <- c(1, 2)
> b <- c(3, 4)
> t(a) %*% b
[,1]
[1,] 11
> a %*% t(b)
[,1] [,2]
[1,] 3 4
[2,] 6 8
> a %*% b
[,1]
[1,] 11
you
【在 q**j 的大作中提到】 : i see what you are saying. but the second one does not work without t(). you : will see if you try it.
| q**j 发帖数: 10612 | 20 sorry, i mean:
sum(t(a)%*%(a>K))+sum(t(rep(K))%*%(a
the second one won't work without t(). | | | s*****n 发帖数: 2174 | 21 That one missed "times=" argument in the rep(),
Once you fix it, it should work with or without t().
But in terms of data readibility, I suggest keeping t().
> a <- c(3,6,2,5,7,9)
> K <- 4
> sum(a %*% (a > K))+sum(rep(K, time = length(a)) %*% (a < K))
[1] 35
> sum(t(a) %*% (a > K))+sum(t(rep(K, time = length(a))) %*% (a < K))
[1] 35
【在 q**j 的大作中提到】 : sorry, i mean: : sum(t(a)%*%(a>K))+sum(t(rep(K))%*%(a: the second one won't work without t().
| q**j 发帖数: 10612 | 22 right, the question is why R knows how many times to rep with t(), but does
not know it without t()? | s*****n 发帖数: 2174 | 23 which version of R are you using?
I am using 2.3.1, and on my machine, missing "times=" always pump me an
error,
no matter whether I have t() outside or not.
does
【在 q**j 的大作中提到】 : right, the question is why R knows how many times to rep with t(), but does : not know it without t()?
| q**j 发帖数: 10612 | | s*****n 发帖数: 2174 | 25 Maybe they made R smarter in the later versions.
Personally, I do not like any ambiguous codes, so
I won't write codes like that anyway.
It is like the case when they allow people to use
"=" to do assignment. To me, it is the worst decision
ever made in the history of R.
【在 q**j 的大作中提到】 : i am using 2.8.1
|
|