简体   繁体   English

使用python矩阵M(使用numpy),如何将该矩阵的每一列与所有其他列进行比较?

[英]With a python matrix M (using numpy), how can I compare each column of that matrix with all other columns?

I want to see whether there is a way to compare a column of a matrix with all other columns of the same matrix, without using loops. 我想看看是否有一种方法可以不使用循环就将矩阵的一列与同一矩阵的所有其他列进行比较。 Obviously the result can be extremely large and for a [mxn] matrix will grow in size with n**2. 显然,结果可能会非常大,对于[mxn]矩阵,其大小将随着n ** 2的增长而增加。 But with reasonable values n it is hopefully possible. 但是,只要具有合理的值n,就有可能实现。

Concretely I have a matrix like: 具体来说,我有一个矩阵,如:

|  1  -1  1  1  -1 |
| -1  -1  1  1   1 |
| -1   1 -1 -1   1 |
|  1  -1  1 -1   1 |

And need to get AND and XOR values (or other logical comparisons) for all combinations of columns. 并且需要获取所有列组合的AND和XOR值(或其他逻辑比较)。 Is this possible without loops? 这可能没有循环吗? I am relatively new to Python, R and Octave, but have encountered similar problems already several times. 我对Python,R和Octave相对较新,但是已经多次遇到类似的问题。 So a solution in Python or other languages would be most welcome! 因此,最欢迎使用Python或其他语言的解决方案!

Example: 例:

| 1 -1  1 |     | 2 0 2 0 -2 0 2 0  1 | 
| 1  1 -1 |  => | 2 2 0 2  2 0 0 0 -2 |
|-1  1  1 |     |-2 0 0 0  2 2 0 2  2 |

[ column 1 + matrix , column 2 + matrix , column 3 + matrix ] [第1列+矩阵,第2列+矩阵,第3列+矩阵]

It's getting big very fast. 它变得非常快。 There is a lot of duplication, for example I do not need to have a column added to itself, and I do not really need to compare each column with all other columns, but better with all the columns with higher column number (on the right side). 有很多重复项,例如,我不需要在其自身上添加一列,也不需要将每个列与所有其他列进行比较,而对于列号较高的所有列则更好(右侧)侧)。 Perhaps it is easier to implement it like example above. 也许像上面的示例一样容易实现。 And maybe it is just not possible! 也许这是不可能的! I can imagine this is the kind of function that quickly absorbs all available memory! 我可以想象这是一种快速吸收所有可用内存的功能!

I don't know whether there is some inbuilt mechanism for your problem solution or not. 我不知道您的问题解决方案是否有内置的机制。 But I have something which may helpful to you. 但是我有一些可能对您有所帮助。

import numpy as np
a = np.random.random_integers(0,6,(300,300))
b = np.random.random_integers(0,6,(300,300))

for i in xrange(300):
    a = np.roll(a, 1, axis=a)
    ###Do whatever you want to do with a and b

here all combination of matrix a will be generated so you can use it as per your use. 这里将生成矩阵a的所有组合,因此您可以根据需要使用它。

And you don't want to use loops may be because of time efficiency. 而且您不想使用循环可能是因为时间效率高。 but this will work faster because of numpy improved library 但是由于numpy改进了库,因此可以更快地工作

I'm writing the answer on StackOverflow for the first time. 我是第一次在StackOverflow上写答案。 So, if you find it's wrong then sorry! 因此,如果您发现错误,那就对不起!

refrance: http://docs.scipy.org/doc/numpy/reference/generated/numpy.roll.html 弗朗西斯科: http ://docs.scipy.org/doc/numpy/reference/generated/numpy.roll.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中,如何将 numpy 数组与矩阵的每一行进行比较以选择与向量最相似的行? - In Python, how can I compare a numpy array to each row of a matrix to chose the row that is most similar to the vector? 在Python中删除NumPy矩阵的列,并保持所有其他列的索引不变 - Delete column of a NumPy matrix in Python keeping index of all other columns intact 使用Numpy将3D矩阵中的最后一列设置为所有其他列的总和 - Setting the last column in a 3D matrix to the sum of all the other columns using Numpy 仅使用纯 python 将矩阵中的每一行与每一列进行比较 - compare each row with each column in matrix using only pure python 如何使用 numpy 添加矩阵的所有元素? - How can I add all the elements of a Matrix using numpy? 如何使用numpy和python将函数应用于矩阵的一行或一列 - How can I apply a function to a single row or column of a matrix using numpy and python 如何在 python 的矩阵中打印特定值? (使用 numpy) - How can i print a specific value in matrix in python? (Using numpy) 将 numpy 矩阵的列与数组进行比较 - Compare columns of numpy matrix with array 如何在没有 NumPy 的情况下将矩阵的行转换为 Python 中的列? - How do I convert the rows of a matrix to columns in Python without NumPy? 使用 numpy 的 Python 矩阵交换列 - Python matrix swapping columns using numpy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM