Checking if a matrix is symmetric in Numpy
You can simply compare it to its transpose using allclose def check_symmetric(a, rtol=1e-05, atol=1e-08): return numpy.allclose(a, a.T, rtol=rtol, atol=atol)
You can simply compare it to its transpose using allclose def check_symmetric(a, rtol=1e-05, atol=1e-08): return numpy.allclose(a, a.T, rtol=rtol, atol=atol)
Okay, I’m a dimwit. Here is how it goes: There is the function convertTo that does exactly what I want. Thanks for matrix type conversion in opencv for pointing this out. Here is how I do it: cv::Mat A = loadMat(“mymat.xml”); // See function loadMat in the question! A.convertTo(A, CV_64F);
You can create this sort of plot yourself pretty easily using the built-in functions imagesc and text and adjusting a number of parameters for the graphics objects. Here’s an example: mat = rand(5); % A 5-by-5 matrix of random values from 0 to 1 imagesc(mat); % Create a colored plot of the matrix values colormap(flipud(gray)); … Read more
There are a couple ways you can do this depending on how you want to deal with repeated values. Here’s a solution that finds indices for the 5 largest values (which could include repeated values) using sort: [~, sortIndex] = sort(A(:), ‘descend’); % Sort the values in descending order maxIndex = sortIndex(1:5); % Get a … Read more
The way most languages store multi-dimensional arrays is by doing a conversion like the following: If matrix has size, n (rows) by m (columns), and we’re using “row-major ordering” (where we count along the rows first) then: matrix[ i ][ j ] = array[ i*m + j ]. Here i goes from 0 to (n-1) … Read more
To do this in NumPy, without using a double loop, you can use tril_indices. Note that depending on your matrix size, this may be slower that adding the transpose and subtracting the diagonal though perhaps this method is more readable. >>> i_lower = np.tril_indices(n, -1) >>> matrix[i_lower] = matrix.T[i_lower] # make the matrix symmetric Be … Read more
The number of rows of a list of lists would be: len(A) and the number of columns len(A[0]) given that all rows have the same number of columns, i.e. all lists in each index are of the same size.