matrix

Why does it print twice the “big” diagonal (matrix)

寵の児 提交于 2020-01-23 12:05:51
问题 I have a matrix like the following: ` matrix = [ ['P', 'o', 'P', 'o', 'P'], ['m', 'i', 'c', 's', 'r'], ['g', 'a', 'T', 'A', 'C'], ['n', 'n', 'e', 'r', 't'], ['a', 'g', 'o', 'd', 'o'], ['a', 'p', 'p', 'l', 'e'] ]` and this code which prints every letter in a 'diagonal rising to the right' way with repetitions: `test_word = '' for upper in range(len(matrix)): for rep1 in range(min(upper + 1, len(matrix[0]))): for rep2 in range(rep1, min(upper + 1, len(matrix[0]))): for j in range(rep1, rep2 + 1

Why does it print twice the “big” diagonal (matrix)

假如想象 提交于 2020-01-23 12:05:36
问题 I have a matrix like the following: ` matrix = [ ['P', 'o', 'P', 'o', 'P'], ['m', 'i', 'c', 's', 'r'], ['g', 'a', 'T', 'A', 'C'], ['n', 'n', 'e', 'r', 't'], ['a', 'g', 'o', 'd', 'o'], ['a', 'p', 'p', 'l', 'e'] ]` and this code which prints every letter in a 'diagonal rising to the right' way with repetitions: `test_word = '' for upper in range(len(matrix)): for rep1 in range(min(upper + 1, len(matrix[0]))): for rep2 in range(rep1, min(upper + 1, len(matrix[0]))): for j in range(rep1, rep2 + 1

Largest equal submatrices in bigger matrix

六眼飞鱼酱① 提交于 2020-01-23 12:04:36
问题 Suppose you have one matrix of height h and width w (h,w <= 500) You have to find two submatrices of same size that are equal. Any idea for solving ? 回答1: You can enumerate all vectors (-h < dh < h, 0 <= dw < w) in O(hw) . For each such vector, translate the matrix by the vector (dh, dw) and subtract the translated matrix from the original one. In the range where they overlap you want to find the rectangle of zeroes that has the largest area. You can use a linear time algorithm to do that.

Getting 1D Subsets of Multi dimensional arrays in julia

走远了吗. 提交于 2020-01-23 11:54:32
问题 I have a multi dimensional array in julia: julia> ac.value 3x100x3 Array{Float64,3}: [:, :, 1] = 0.29238 0.0751815 0.00843636 … -0.0143826 0.0403283 0.0225896 0.263146 0.080687 0.000462262 -0.00635778 0.0307563 0.0379104 0.992458 0.986423 0.980587 0.561173 0.55516 0.549105 [:, :, 2] = 0.362155 0.13406 0.0741124 … 0.0231614 0.0156455 0.0121797 0.325581 0.11181 0.0447847 0.0098042 0.0193873 0.0146943 0.914888 0.852297 0.796608 -0.0500265 -0.0551787 -0.0520171 [:, :, 3] = 0.269976 0.108082 0

In Julia, How can I column-normalize a sparse matrix?

怎甘沉沦 提交于 2020-01-23 08:34:26
问题 If I have constructed a sparse matrix using the sparse(i, j, k) constructor, how can I then normalize the columns of the matrix (so that each column sums to 1)? I cannot efficiently normalize the entries before I create the matrix, so any help is appreciated. Thanks! 回答1: The easiest way would be a broadcasting division by the sum of the columns: julia> A = sprand(4,5,.5) A./sum(A,1) 4x5 Array{Float64,2}: 0.0 0.0989976 0.0 0.0 0.0795486 0.420754 0.458653 0.0986313 0.0 0.0 0.0785525 0.442349 0

R: make symmetric matrix from lower diagonal [duplicate]

一笑奈何 提交于 2020-01-23 06:16:15
问题 This question already has answers here : Creating a symmetric matrix in R (4 answers) Make a matrix symmetric (1 answer) Closed 4 years ago . I have a lower triangular of a matrix that I'm trying to convert to a dissim matrix and thus it needs to be symmetric. print(rdf) X0 X1 X2 X3 X4 0 0.0000000 NA NA NA NA 1 0.5340909 0.0000000 NA NA NA 2 0.5340909 0.0000000 0.0000000 NA NA 3 0.3200000 0.5227273 0.5227273 0.0000000 NA 4 0.6263736 0.4945055 0.4945055 0.5384615 0 library(gdata) upperTriangle

Create rolling covariance matrix in pandas

心不动则不痛 提交于 2020-01-23 03:38:07
问题 I am trying to create a set of rolling covariance matrices on financial data (window size = 60). Returns is a 125x3 df. import pandas as pd roll_rets = returns.rolling(window=60) Omega = roll_rets.cov() Omega is a 375x3 data frame with what looks like a multi-index - i.e. there are 3 values for each timestamp. What I actually want this to return is a set of 66 3x3 covariance matrices (i.e. one for each period), but I can't work out how to iterate over returns correctly to do this. I think I'm

Matlab OOP accessing properties from an object array

*爱你&永不变心* 提交于 2020-01-23 02:05:24
问题 New to Matlab come from C/C++...... I have an array of objects and I'm trying to access the values of every single object in the array and concatenate them into one variable. Class sample properties(GetAccess = 'public', SetAccess ='public') ID; Value; end methods function obj = sample(id, value) obj.ID = id; obj.Value = value; end end end Then I make an matrix containing some of the objects. x = sample.empty(3,0); x(1) = sample(1,3); x(2) = sample(1,4); x(3) = sample(1,5); Then I want to get

Looping through diagonal+1 of a matrix

╄→尐↘猪︶ㄣ 提交于 2020-01-23 01:43:25
问题 I need to loop through the diagonal+1 (i.e. the values 1 column to the right of the diagonal) and write the value to a column in a dataframe: write.csv(data.frame(matrix[1,2], matrix[2,3], matrix[3,4]) How can I do this using a function, rather than just listing all the positions of the values? 回答1: You can index using a matrix. eg m <- matrix(1:25, ncol = 5) The off diagonals can be accessed using offd <- cbind(1:4,2:5) m[offd] ## [1] 6 12 18 24 You could create a function that does this

Why does the Jama matrix inner dimension agree in the first iteration, but later it does not?

吃可爱长大的小学妹 提交于 2020-01-23 00:48:10
问题 Following Jama Matrices are defined in my code: P: 3*3 Matrix I: 3*3 identity Matrix K: 3*2 Matrix H: 2*3 Matrix Q: 3*3 Matrix Following is my code snippet: private Matrix getP() { P= (I.minus(K.times(H))).times(Q); Log.d("csv", "P is calculated"); return P; } While running the code, at first iteration it works, i.e, P is calculated is printed at the Logcat. However, it happens only once and the application gets stopped. Following is the error: java.lang.IllegalArgumentException: Matrix inner