向量元素所有组合之间的乘积 [英] Product between all combinations of a vector's elements

查看:82
本文介绍了向量元素所有组合之间的乘积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个没有重复值的向量c(1, 2, 3, 4).我需要一个向量c(1 * 2, 1 * 3, 1 * 4, 2 * 3, 2 * 4, 3 * 4),所以乘法是在此向量值的所有可能组合中完成的.有办法吗?预先感谢!

Suppose I have a vector c(1, 2, 3, 4) with no duplicated values. I need a vector c(1 * 2, 1 * 3, 1 * 4, 2 * 3, 2 * 4, 3 * 4), so the multiplication is done in all possible combinations of this vector's values. Is there a way of doing that? Thanks in advance!

推荐答案

我们可以将combn与匿名函数调用一起使用

We can use combn with anonymous function call

combn(vec, 2, FUN = function(x) x[1] * x[2])
#[1]  2  3  4  6  8 12

数据

vec <- 1:4

这篇关于向量元素所有组合之间的乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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