生成二元矩阵的所有可能的排列(Generate all possible permutations of a binary matrix)

编程入门 行业动态 更新时间:2024-10-25 20:25:01
生成二元矩阵的所有可能的排列(Generate all possible permutations of a binary matrix)

我期望生成所有可能的4x4矩阵,其中每个元素可以是0或1。

R中有这样一个功能吗?

I'm looking to generate all possible 4x4 matrices, where each element can either be a 0 or a 1.

Is there a function in R to do this?

最满意答案

这是一个函数,可以为0到2^16-1索引创建这样的矩阵:

num2mat = function(num){ matrix(as.integer(intToBits(num)),4,4) }

这是它产生的结果:

> num2mat(0) [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 > num2mat(2^15+2^13+2^10+2^8+2^7+2^5+2^2+1) [,1] [,2] [,3] [,4] [1,] 1 0 1 0 [2,] 0 1 0 1 [3,] 1 0 1 0 [4,] 0 1 0 1 > num2mat(2^16-1) [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 1 1 1 1 [3,] 1 1 1 1 [4,] 1 1 1 1

Here is a function that would create such matrices for indices from 0 to 2^16-1:

num2mat = function(num){ matrix(as.integer(intToBits(num)),4,4) }

Here is what it produces:

> num2mat(0) [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 > num2mat(2^15+2^13+2^10+2^8+2^7+2^5+2^2+1) [,1] [,2] [,3] [,4] [1,] 1 0 1 0 [2,] 0 1 0 1 [3,] 1 0 1 0 [4,] 0 1 0 1 > num2mat(2^16-1) [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 1 1 1 1 [3,] 1 1 1 1 [4,] 1 1 1 1

更多推荐

本文发布于:2023-08-06 04:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1443611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   排列   Generate   matrix   binary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!