logo
down
shadow

c++ armadillo cast/convert to integer type vector or matrix


c++ armadillo cast/convert to integer type vector or matrix

By : unsure_undergrad
Date : November 17 2020, 04:28 AM
To fix the issue you can do If you are just dealing with positive values, then the conv_to function of the armadillo package will do exactly the same as the method you are trying to use.
code :
vec t = linspace(0, 100);
double freq = 0.25;
ivec indices = conv_to<ivec>::from(t / freq);
ivec indices = conv_to<ivec>::from(floor(t / freq));


Share : facebook icon twitter icon
vector into matrix armadillo

vector into matrix armadillo


By : ricxsar
Date : March 29 2020, 07:55 AM
wish of those help It shouldn't be so hard to achieve similar behavior with reshape I think:
code :
mat vec2mat(vec V, size_t cols) {
    size_t rows = std::ceil(V.n_elems / double(cols));
    return V.reshape(cols, rows);// return the original vector as matrix
}
How do I convert an armadillo matrix to a vector of vectors?

How do I convert an armadillo matrix to a vector of vectors?


By : user3336040
Date : March 29 2020, 07:55 AM
Hope this helps In such cases you should use arma::conv_to which is a totally superb feature of arma.
Note that this method will require from a source object to be able to be interpreted as a vector. That is why we need to do this iteratively for every row. Here is a conversion method:
code :
#include <armadillo>

typedef std::vector<double> stdvec;
typedef std::vector< std::vector<double> > stdvecvec;

stdvecvec mat_to_std_vec(arma::mat &A) {
    stdvecvec V(A.n_rows);
    for (size_t i = 0; i < A.n_rows; ++i) {
        V[i] = arma::conv_to< stdvec >::from(A.row(i));
    };
    return V;
}
#include <iomanip>
#include <iostream>

int main(int argc, char **argv) {
    arma::mat A = arma::randu<arma::mat>(5, 5);
    std::cout << A << std::endl;

    stdvecvec V = mat_to_std_vec(A);
    for (size_t i = 0; i < V.size(); ++i) {
        for (size_t j = 0; j < V[i].size(); ++j) {
            std::cout << "   "
                << std::fixed << std::setprecision(4) << V[i][j];
        }
        std::cout << std::endl;
    }
    return 0;
}
0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567

0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567
Efficient copying/casting of large matrix std::vector<std::vector<double>> and armadillo arma::mat

Efficient copying/casting of large matrix std::vector<std::vector<double>> and armadillo arma::mat


By : Christina Renteria
Date : March 29 2020, 07:55 AM
around this issue You can probably avoid the copy by changing a bit the way you use your vectors/memory.
If you look at the documentation
Idiomatic matrix type conversion, say convert Integer (0/1) matrix to boolean matrix

Idiomatic matrix type conversion, say convert Integer (0/1) matrix to boolean matrix


By : Dmitry Shelya
Date : March 29 2020, 07:55 AM
Hope that helps mode(B) <- "logical" or "mode<-"(B, "logical"). We can also use storage.mode function.
This workaround is good for two reasons:
code :
## an integer matrix
A <- matrix(1:4, nrow = 2, dimnames = list(letters[1:2], LETTERS[1:2]))
#  A B
#a 1 3
#b 2 4
"mode<-"(A, "numeric")
#  A B
#a 1 3
#b 2 4

"mode<-"(A, "logical")
#     A    B
#a TRUE TRUE
#b TRUE TRUE

"mode<-"(A, "chracter")
#  A   B  
#a "1" "3"
#b "2" "4"

"mode<-"(A, "complex")
#     A    B
#a 1+0i 3+0i
#b 2+0i 4+0i

str("mode<-"(A, "list"))  ## matrix list
#List of 4
# $ : int 1
# $ : int 2
# $ : int 3
# $ : int 4
# - attr(*, "dim")= int [1:2] 2 2
# - attr(*, "dimnames")=List of 2
#  ..$ : chr [1:2] "a" "b"
#  ..$ : chr [1:2] "A" "B"
f <- factor(c("a", "b"))

## this "surprisingly" doesn't work
mode(f) <- "character"
#Error in `mode<-`(`*tmp*`, value = "character") : 
#  invalid to change the storage mode of a factor

## this also doesn't work
mode(f) <- "numeric"
#Error in `mode<-`(`*tmp*`, value = "numeric") : 
#  invalid to change the storage mode of a factor

## this does not give any error but also does not change anything
## because a factor variable is internally coded as integer with "factor" class
mode(f) <- "integer"
f
#[1] a b
#Levels: a b
How do i convert a vector into armadillo matrix?

How do i convert a vector into armadillo matrix?


By : Allen Gaminde
Date : March 29 2020, 07:55 AM
help you fix your problem I have a vector that contains a list of integers. How do i convert that vector into an armadillo matrix with only 1 column and the number or rows as the length of the vector? , I think you can use:
code :
mat A = conv_to<mat>::from(your_vector)
Related Posts Related Posts :
  • Vector of pointers: some clarification needed
  • scaling a bitmap without losing quality
  • How to evaluate a function directly?
  • QNetworkAccessManager: How to change post data at createRequest function
  • Returning std::move of a local variable
  • Is there a fast linear SVM library with a good C++ interface?
  • What is the purpose of name mangling in C++?
  • C++ Why the void function is not working eventhough I called it?
  • How the pointer and the pointer's adress can share the same memory adress?
  • error C2220: warning treated as error - no 'object' file generated
  • exception of strcpy function in c++ console program
  • How to access USB barcode scanner data directly from USB port using C/C++ without driver
  • Under which circumstances will std::vector.clear() call a destructor?
  • Template partial specialisation and dependent names
  • best way to share data between c codes
  • C++ ignores if statement conditions
  • How to set the argv[ ] to be case-insensitive in a Win32 Console Application?
  • How to fix error "clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)"?
  • C++ How do I print a .txt file verbatim?
  • Creating two dimensional array of class
  • How do I correctly use COMMTIMEOUTS with OVERLAPPED IO mode reading from a Serial port
  • An assert macro which expands to static_assert when possible?
  • How to write a copyconstructor for a class which has HANDLE as a member in win32 c++ application?
  • C++ saving info such as tree in a file
  • Transforming an expression template tree
  • How to overload an operator with multiple parameters like a + b + c?
  • C++ 11 with Raspberry Pi performances
  • Make a C++ class look like a numpy array using swig
  • Postfix incrementer overloading for nested enumerated types
  • Is there a tidy way of associating metadata with functions in C++
  • QObject::installEventFilter(): Cannot filter events for objects in a different thread
  • LNK2005 error with Zxing C++
  • 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
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org