matrix

Check if vector in a matrix

末鹿安然 提交于 2020-07-22 06:39:05
问题 I have a matrix new<-matrix(9,4,4) new[1,1]<-0 v1<-c(0,0) new thus looks like this: 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 I now want to check if this matrix contains the vector v1. So I did v1 %in% new and obtain TRUE TRUE although I would like to check the whole vector of two zeros in a row/ column and thus would like to get a FALSE. 回答1: You can use rollapply from zoo to test if matrix contains a vector : library(zoo) any(apply(new, 2, rollapply, length(v1), identical, v1)) #[1] FALSE new[2,1] <-

How to extract optimization problem matrices A,b,c using JuMP in Julia

孤人 提交于 2020-07-22 06:00:17
问题 I create an optimization model in Julia-JuMP using the symbolic variables and constraints e.g. below using JuMP using CPLEX # model Mod = Model(CPLEX.Optimizer) # sets I = 1:2; # Variables x = @variable( Mod , [I] , base_name = "x" ) y = @variable( Mod , [I] , base_name = "y" ) # constraints Con1 = @constraint( Mod , [i in I] , 2 * x[i] + 3 * y[i] <= 100 ) # objective ObjFun = @objective( Mod , Max , sum( x[i] + 2 * y[i] for i in I) ) ; # solve optimize!(Mod) I guess JuMP creates the problem

How to solve linear programming model in R

大兔子大兔子 提交于 2020-07-19 19:13:32
问题 I need to solve the following microeconomic problem: I have six assets I can produce (asset 1 - 6) across five years (2011 - 2015). Each asset can only be produced during one year. Each asset must be produced in my five year period. Production is not mutually exclusive; I can produce more than one good in a year without affecting the production of either. Each asset has a fixed cost of production equal to 30. I must have non-negative profit in each year; revenues must be at least 30. Below is

How to solve linear programming model in R

耗尽温柔 提交于 2020-07-19 19:10:11
问题 I need to solve the following microeconomic problem: I have six assets I can produce (asset 1 - 6) across five years (2011 - 2015). Each asset can only be produced during one year. Each asset must be produced in my five year period. Production is not mutually exclusive; I can produce more than one good in a year without affecting the production of either. Each asset has a fixed cost of production equal to 30. I must have non-negative profit in each year; revenues must be at least 30. Below is

CUDA matrix transpose with shared memory

那年仲夏 提交于 2020-07-16 08:04:49
问题 I need to implement a matrix transpose function on a GPU using shared memory. I have done it in a simple way without shared memory which works fine and also an attempt with SM. But unfortunately the calculation is not correct and I can not figure out why. A complete working example can be found here and at the bottom of this question. EDIT 1 I further know that the first index of the result where I have a wrong value is index 32 (of the flatted matrix, so matr[0][32] in a two dimensional

CUDA matrix transpose with shared memory

不羁岁月 提交于 2020-07-16 08:04:30
问题 I need to implement a matrix transpose function on a GPU using shared memory. I have done it in a simple way without shared memory which works fine and also an attempt with SM. But unfortunately the calculation is not correct and I can not figure out why. A complete working example can be found here and at the bottom of this question. EDIT 1 I further know that the first index of the result where I have a wrong value is index 32 (of the flatted matrix, so matr[0][32] in a two dimensional

Set a value of a specific column for each row of a matrix

折月煮酒 提交于 2020-07-16 05:49:13
问题 I have a matrix A with m rows and I'd like to set a specific element of each row equal 1. The column index varies from row to row and is specified by a column vector a (with m values). That is, I want A_{i,a_i} = 1 . Is there a quick way to do this in Matlab (without a for-loop)? 回答1: I solved it using the sub2ind function: A(sub2ind(size(A), 1:numel(a), a')) = 1 来源: https://stackoverflow.com/questions/20957317/set-a-value-of-a-specific-column-for-each-row-of-a-matrix

Set a value of a specific column for each row of a matrix

情到浓时终转凉″ 提交于 2020-07-16 05:48:09
问题 I have a matrix A with m rows and I'd like to set a specific element of each row equal 1. The column index varies from row to row and is specified by a column vector a (with m values). That is, I want A_{i,a_i} = 1 . Is there a quick way to do this in Matlab (without a for-loop)? 回答1: I solved it using the sub2ind function: A(sub2ind(size(A), 1:numel(a), a')) = 1 来源: https://stackoverflow.com/questions/20957317/set-a-value-of-a-specific-column-for-each-row-of-a-matrix

Can eigen matrix solver use multi threads and how?

Deadly 提交于 2020-07-10 10:22:11
问题 When factorize a 34918 x 34918 sparse matrix, it takes about 1'20" to factorize it (only a few sec to build the sparse matrix). Can sparse matrix be factorized with multi threads? (visual studio) Here is the code: Eigen::SparseMatrix<double> laplacian(verticesNum, verticesNum); // reserve space for non-zero elements in sparse matrix laplacian.reserve(Eigen::VectorXi::Constant(verticesNum, 20)); // build matrix operator for (int i = 0; i < vertices->size(); i++) { // ... some algorithm } //

scipy sparse matrix sum results in a dense matrix - how to enforce result sparseness?

北城余情 提交于 2020-07-09 12:50:07
问题 Summing over one axis of a scipy.sparse.csr_matrix results in a numpy.matrix object. Given that my sparse matrix is really sparse, I find extremely annoying this behaviour. Here is an example: dense = [[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 2., 0., 4., 0., 0.]] from scipy.sparse import csr_matrix sparse = csr_matrix(dense) print(sparse.sum(1)) with result: matrix([[ 0.], [ 1.], [ 0.], [ 0.], [ 6.]]) How can I enforce sparseness in the sum