matrix

Efficiency of matrix rowSums() vs. colSums() in R vs Rcpp vs Armadillo

瘦欲@ 提交于 2020-01-01 08:53:33
问题 Background Coming from R programming, I'm in the process of expanding to compiled code in the form of C/C++ with Rcpp . As a hands on exercise on the effect of loop interchange (and just C/C++ in general), I implemented equivalents to R's rowSums() and colSums() functions for matrices with Rcpp (I know these exist as Rcpp sugar and in Armadillo -- this was just an exercise). Question I have my C++ implementation of rowSums() and colSums() along with Rcpp sugar and arma::sum() versions in this

Can I use my own Python class with numpy or some other matrix library?

邮差的信 提交于 2020-01-01 08:47:07
问题 I'd like to be able to do matrix operations using a Python class as the elements—in this case, a simple Galois field implementation. It implements the necessary __add__ , __mul__ , __sub__ etc. At first, I thought this should be possible with numpy arrays, using the dtype parameter, but from the dtype documentation, it seems that dtype can't be an arbitrary Python class. For example, I have a class Galois which does operations modulo 2: >>> from galois import Galois >>> Galois(1) + Galois(0)

Can I use my own Python class with numpy or some other matrix library?

◇◆丶佛笑我妖孽 提交于 2020-01-01 08:47:02
问题 I'd like to be able to do matrix operations using a Python class as the elements—in this case, a simple Galois field implementation. It implements the necessary __add__ , __mul__ , __sub__ etc. At first, I thought this should be possible with numpy arrays, using the dtype parameter, but from the dtype documentation, it seems that dtype can't be an arbitrary Python class. For example, I have a class Galois which does operations modulo 2: >>> from galois import Galois >>> Galois(1) + Galois(0)

Opencv multiply scalar and matrix

戏子无情 提交于 2020-01-01 07:59:47
问题 I have been trying to achieve something which should pretty trivial and is trivial in Matlab . I want to simply achieve something such as: cv::Mat sample = [4 5 6; 4 2 5; 1 4 2]; sample = 5*sample; After which sample should just be: [20 24 30; 20 10 25; 5 20 10] I have tried scaleAdd , Mul , Multiply and neither allow a scalar multiplier and require a matrix of the same "size and type". In this scenario I could create a Matrix of Ones and then use the scale parameter but that seems so very

Searching for largest rectangle with even count of numbers in matrix

做~自己de王妃 提交于 2020-01-01 07:22:28
问题 I have an a*b matrix of numbers (from 0 to 9), for example [[1,2,3], [4,5,6], [7,6,5]] And I want to find the largest rectagle, where is count of each number even (in this example, the result will be) [[5,6], [6,5]] -- numbers 5 an 6 are here 2 times Is here any way, how to find it in n*log(n) or better time where n is count of items in matrix (n = a*b)? (If that rectagle doesn't exist, the result will be an empty set) thanks 回答1: You can create a second matrix in O(N) time (where N is the

sum matrix columns in python

牧云@^-^@ 提交于 2020-01-01 07:18:07
问题 I can sum the items in column zero fine. But where do I change the code to sum column 2, or 3, or 4 in the matrix? I'm easily stumped. def main(): matrix = [] for i in range(2): s = input("Enter a 4-by-4 matrix row " + str(i) + ": ") items = s.split() # Extracts items from the string list = [ eval(x) for x in items ] # Convert items to numbers matrix.append(list) print("Sum of the elements in column 0 is", sumColumn(matrix)) def sumColumn(m): for column in range(len(m[0])): total = 0 for row

How to bin a 2D array in numpy?

冷暖自知 提交于 2020-01-01 06:56:15
问题 I'm new to numpy and I have a 2D array of objects that I need to bin into a smaller matrix and then get a count of the number of objects in each bin to make a heatmap. I followed the answer on this thread to create the bins and do the counts for a simple array but I'm not sure how to extend it to 2 dimensions. Here's what I have so far: data_matrix = numpy.ndarray((500,500),dtype=float) # fill array with values. bins = numpy.linspace(0,50,50) digitized = numpy.digitize(data_matrix, bins)

convert openCV matrix into vector

大城市里の小女人 提交于 2020-01-01 06:11:10
问题 Looks deceptively easy. After all we know that an std or openCV vector can be easily converted into Matrix like this: vector<Point> iptvec(10); Mat iP(iptvec); The reverse is suggested in openCV cheatSheet: vector<Point2f> ptvec = Mat_ <Point2f>(iP); However, there is one caveat: the matrix has to have only one row or one column. To convert an arbitrary matrix you have to reshape: int sz = iP.cols*iP.rows; vector<Point2f> ptvec = Mat <Point2f>(iP.reshape(1, sz)); Otherwise you will get an

Transform Correlation Matrix into dataframe with records for each row column pair

倖福魔咒の 提交于 2020-01-01 04:54:08
问题 I have a large matrix of correlations (1093 x 1093). I'm trying my matrix into a dataframe that has a column for every row and column pair, so it would (1093)^2 records. Here's a snippet of my matrix 60516 45264 02117 60516 1.00000000 -0.370793012 -0.082897941 45264 -0.37079301 1.000000000 0.005145601 02117 -0.08289794 0.005145601 1.000000000 The goal from here would be to have a dataframe that looks like this: row column correlation 60516 60516 1.000000000 60516 45264 -0.370793012 ........

Normalize rows of a matrix within range 0 and 1

自古美人都是妖i 提交于 2020-01-01 04:09:45
问题 I am trying to normalize all rows of my matrix data at once within range 0 and 1. But I don't know how to do it.. For example, I want to normalize each "obs1", "obs2", "obs3". Thus, minimum, maximum, and sum of each "obs1", "obs2", "obs3" will be used. My data format is, `mydata a b c d e obs1 8.15609 11.5379 11.1401 8.95186 7.95722 obs2 339.89800 856.3470 691.3490 590.28600 543.67200 obs3 2.12776 46.4561 136.8860 118.09100 119.86400 ` Also, When I searched to perform this, people used