With these it helps As you have noticed yourself, boost::numeric::ublas::matrix doesn't resize itself automatically, like std::vector. You have to do the resizing manually before calling operator(), or write a function template that does the resizing for you as shown here: code :
namespace ublas = boost::numeric::ublas; //just a friendly alias!
template<typename T, typename U>
void assign(ublas::matrix<T>& m,std::size_t r,std::size_t c,U const& data)
{
m.resize(std::max(m.size1(), r+1), std::max(m.size2(), c+1));
m(r, c) = data;
}
int main ()
{
ublas::matrix<double> m;
assign(m, 0, 0, 9); //m(0,0) = 9;
assign(m, 3, 2, 20); //m(3,2) = 20
std::cout << m << std::endl; //prints: [4,3]((9,0,0),(0,0,0),(0,0,0),(0,0,20))
return 0;
}
Share :

elementwise operations with boost c++ ublas matrix and vector types
By : sharepointlearner201
Date : March 29 2020, 07:55 AM
I hope this helps . WARNING The following answer is incorrect. See Edit at the bottom. I've left the original answer as is to give context and credit to those who pointed out the error. code :
using namespace boost::numeric::ublas;
// Create a 30 element vector of doubles
vector<double> vec(30);
// Assign 8.0 to each element.
std::fill(vec.begin(), vec.end(), 8.0);
// Perform the "Gamma" function on each element and assign the result back
// to the original element in the vector.
std::transform(vec.begin(), vec.end(), vec.begin(), boost::math::tgamma);
matrix<double> mat(30, 30);
.
.
.
std::transform(mat.begin1(), mat.end1(), mat.begin1(), boost::math::tgamma);

Dynamic allocation of sparse matrix using uBLAS
By : hopezjj
Date : March 29 2020, 07:55 AM
will be helpful for those in need The memory seems to be allocated dynamically anyway, so I can just do: code :
int n;
n = 10;
compressed_matrix<double> m (n,n,n);

Eigen3: Passing fixed size matrix and dynamic size matrix in one data type in template function
By : Sapna Rai
Date : March 29 2020, 07:55 AM
it should still fix some issue Since the number of columns is only known at runtime (it's not part of the type), you need to specify it when you create the matrix ret as follow: code :
template<typename Derived>
typename Derived::PlainObject bar(const Eigen::MatrixBase<Derived>& v)
{
typename Derived::PlainObject ret(v.rows(), v.cols());
std::cout << "v size : " << v.rows() << ", " << v.cols() << std::endl;
std::cout << "ret size: " << ret.rows() << ", " << ret.cols() << std::endl;
return ret;
}
v size : 3, 1000
ret size: 3, 1000
v size : 3, 1000
ret size: 3, 1000

Insert an element to a variable size matrix at specific locations in R Studio
By : Emil
Date : March 29 2020, 07:55 AM
it fixes the issue I would preallocate a matrix of the correct size, and separately populate the siteCoor and sectorCoor rows. We can use the initial data vector passed to matrix() to populate the siteCoor rows, and then use an indexassignment to populate the sectorCoor rows. code :
res < matrix(siteCoor,nrow(sectorCoor)+nrow(sectorCoor)%/%6L*2L,2L,byrow=T);
res[c(F,rep(T,6L),F),] < sectorCoor;
res;
## [,1] [,2]
## [1,] 1 2
## [2,] 1 19
## [3,] 2 20
## [4,] 3 21
## [5,] 4 22
## [6,] 5 23
## [7,] 6 24
## [8,] 1 2
## [9,] 1 2
## [10,] 7 25
## [11,] 8 26
## [12,] 9 27
## [13,] 10 28
## [14,] 11 29
## [15,] 12 30
## [16,] 1 2
## [17,] 1 2
## [18,] 13 31
## [19,] 14 32
## [20,] 15 33
## [21,] 16 34
## [22,] 17 35
## [23,] 18 36
## [24,] 1 2
N < 3L;
sectorCoor < matrix(seq_len(N*6L*2L),ncol=2L);
siteCoor < matrix(c(1,2),ncol=2L);

I want element (3,2) from a matrix with size (4,4) to try the values of the first column from another matrix with size (
By : Happy
Date : March 29 2020, 07:55 AM
hop of those help? A simple way would be to turn the 3D matrix to 1D vector. You now can write a function that loops over this vector and builds a different permutation of it each time. From each permutation you can reconstruct a 3D matrix. How many possible permutations are there for a vector of size N ? code :
function m = computerDifferentPerm(A)
vec = A(:).';
m = perms(vec);
end

