我想将大小n <batch:job id="testJob" job-repository="jobRepository" parent="jobParent">
<batch:step id="testStep" allow-start-if-complete="true">
<batch:tasklet>
<batch:chunk
reader="testReader"
processor="testProcessor"
writer="jmsWriter">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<task:scheduled-tasks>
<task:scheduled ref="testJobLauncher" method="runJob" cron="0 */5 * * * *"/>
</task:scheduled-tasks>
<bean id="testJobLauncher"
class="com.test.RunScheduler"
p:job-ref="testJob"
p:jobLauncher-ref="jobLauncher"
"/>
@Component
public class RunScheduler {
private JobLauncher jobLauncher;
private Job job;
public void runJob() {
try {
String dateParam = new Date().toString();
JobParameters param = new JobParametersBuilder().addString("date", dateParam).toJobParameters();
JobExecution execution = jobLauncher.run(job, param);
} catch (Exception e) {
LOGGER.error("Can't start job", e);
throw new RuntimeException(e);
}
}
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
public JobLauncher getJobLauncher() {
return jobLauncher;
}
public void setJobLauncher(JobLauncher jobLauncher) {
this.jobLauncher = jobLauncher;
}
}
转换为×n二进制factor
,如果{{1}的第i和第j个元素,其(i,j)元素为1是相同的,否则为0。
以下是实现我想要做的事情的天真方式,但这段代码很慢。有没有更有效的方法来做同样的事情?
matrix
答案 0 :(得分:4)
另一种选择,应该相对较快
tcrossprod(model.matrix( ~ id + 0))
与Hong Ooi的回答类似,您也可以使用稀疏矩阵
library(Matrix)
tcrossprod(sparse.model.matrix( ~ id + 0))
答案 1 :(得分:3)
outer
可用于此目的。
mat <- outer(id, id, "==")
由于输出是二进制矩阵,并且O(N ^ 2)对象有点大,这对于稀疏矩阵来说是一个很好的用例:
library(Matrix)
mat <- Matrix(nrow=100, ncol=100)
mat[] <- outer(id, id, "==") # [] means to assign into the existing 'mat' matrix