logo
down
shadow

How to insert an element into ublas matrix with dynamic size


How to insert an element into ublas matrix with dynamic size

By : Kitteran
Date : November 22 2020, 03:03 PM
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 : facebook icon twitter icon
element-wise operations with boost c++ ublas matrix and vector types

element-wise 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

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

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

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 index-assignment 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 (

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
Related Posts Related Posts :
  • C++ Doubly Linked List with Pointers: Object of class isn't constructed properly
  • Using a random string generator in c++ constructor
  • What should I use instead of void as one of the alternative types in an variant?
  • C++ return value from multithreads using reference
  • How to connect multiple TCP IP clients to same server port using c++
  • Defaul compiler generates the reference operator (In C++)?
  • Unable to change directory time stamp after using FILE_FLAG_BACKUP_SEMANTICS
  • vector handling displaying output
  • WSAGetLastError returns WSAENOTSOCK - Cause?
  • C++: How to overload pow for user type?
  • C++ using arrays as multidimensional despite initalising it as 1D with pointer
  • How negate std::is_integral for use in tag dispatch?
  • Retrieve serial number from USB memory (Windows environment c++)
  • g++ error: invalid preprocessing directive #INCLUDE
  • C++ What is the std::for_each() function parameter type?
  • C++: Read individual lines from text file, sort words alphabetically
  • Saving 'this' address into a variable
  • c++ command line arguments in ubuntu terminal
  • Convert "Cartesian coordinates" to "polar coordinates with respect to user specified origin"
  • In what order are local scoped objects destructed?
  • How to use SDL_MapRGB with SDL 2.0
  • how compiler and interpreter work in case of array declaration
  • GSL integration behaves strange
  • Cropping an image with OpenCV and C
  • Find the last line in text file and select the first 10 char and print to a new file?
  • Created many CCSprits but when triggering ccTouchBegan gives the last one allways
  • seekp and seekg don't work with fstream
  • Taking input in Sublime Text 3
  • ld: -bind_at_load and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together
  • C++ storing #define as std::string would give size as zero compile time
  • How to use static linking with OpenSSL in C/C++
  • What is the difference between a trap, an error, a failure and program abortion?
  • Dynamic members allocation in qt
  • How to reduce object file size when compiling for VxWorks 5.5.1?
  • Printing char by integer qualifier
  • How to write code to be executed before main() gets control?
  • Blocking socket - waitForReadyRead()
  • std::string related errors and handling them, c++
  • VM interpreter - weighting performance benefits and drawbacks of larger instruction set / dispatch loop
  • C / C++ SHIFT / OFFSET / MOVE a Bitmap to the LEFT or RIGHT?
  • Printing numbers column by column
  • How do you change your app icon in visual studio 2013?
  • Fast Screen Transfer
  • c++ Read text file and input the numbers into a 2D array
  • Why are my C++ pointers suddenly diverging?
  • Is there a macro-based adapter to make a functor from a class?
  • CRTP and multilevel inheritance
  • How to implement timer for each object in c++?
  • Stuck when testing custom list class in c++
  • Using each member of class within one function to calculate average
  • check whether if two squares are intersecting with each other
  • Glm Quaternion lookat function
  • Is there guarantee heap allocated block address will not change(implicitly)?
  • Cosine Calculation without cmath library
  • Manually deleting a pointer returned by function
  • Avoid output interleaving
  • C++ error : Expected an identifier
  • Segmentation fault when I call operator new in linux mint
  • Recursively Solving A Sudoku Puzzle Using Backtracking Theoretically
  • lambda closure type and default argument in function template
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org