如何并排显示数据(How to display the data side by side)
我正在尝试使用最简单的代码并排显示数据。
问题是当数据丢失时,右侧没有正确
float
。怎么解决?
.pair { background-color: #ccc; } .pair dt { float: left; width: 90px; text-align: right; color: #999; } .pair dd { margin: 0 0 0 100px; }
<dl class="pair"> <dt>Date</dt> <dd>date goes here</dd> <dt>Country</dt> <dd>USA</dd> <dt>Age</dt> <dd></dd> <dt>Name</dt> <dd></dd> <dt>Other</dt> <dd>other info goes here</dd> </dl>
I'm trying to display the data side by side using the most simple code possible.
The problem is when the data is missing, the right side doesn't
float
correctly.How to fix it?
.pair { background-color: #ccc; } .pair dt { float: left; width: 90px; text-align: right; color: #999; } .pair dd { margin: 0 0 0 100px; }
<dl class="pair"> <dt>Date</dt> <dd>date goes here</dd> <dt>Country</dt> <dd>USA</dd> <dt>Age</dt> <dd></dd> <dt>Name</dt> <dd></dd> <dt>Other</dt> <dd>other info goes here</dd> </dl>
原文:
最满意答案
使用armadillo的
.memptr()
类成员函数,我们可以提取内存指针。 从这里,我们可以使用Eigen的Map<T>()
构造函数来创建一个特征矩阵。现在,我们可以使用
.data()
成员函数从Eigen矩阵中提取一个指向Eigen内存结构的点。 然后,使用arma::mat
的高级构造函数选项,我们可以创建一个armadillo 矩阵 。例如:
#include <RcppArmadillo.h> #include <RcppEigen.h> // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] Eigen::MatrixXd example_cast_eigen(arma::mat arma_A) { Eigen::MatrixXd eigen_B = Eigen::Map<Eigen::MatrixXd>(arma_A.memptr(), arma_A.n_rows, arma_A.n_cols); return eigen_B; } // [[Rcpp::export]] arma::mat example_cast_arma(Eigen::MatrixXd eigen_A) { arma::mat arma_B = arma::mat(eigen_A.data(), eigen_A.rows(), eigen_A.cols(), false, false); return arma_B; } /***R (x = matrix(1:4, ncol = 2)) example_cast_eigen(x) example_cast_arma(x) */
结果:
(x = matrix(1:4, ncol = 2)) # [,1] [,2] # [1,] 1 3 # [2,] 2 4 example_cast_eigen(x) # [,1] [,2] # [1,] 1 3 # [2,] 2 4 example_cast_arma(x) # [,1] [,2] # [1,] 1 3 # [2,] 2 4
快速说一句:如果您使用的是Eigen的映射函数,那么您应该自动对Armadillo矩阵进行更改(反之亦然),例如
#include <RcppArmadillo.h> #include <RcppEigen.h> // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] void map_update(Eigen::MatrixXd eigen_A) { Rcpp::Rcout << "Eigen Matrix on Entry: " << std::endl << eigen_A << std::endl; arma::mat arma_B = arma::mat(eigen_A.data(), eigen_A.rows(), eigen_A.cols(), false, false); arma_B(0, 0) = 10; arma_B(1, 1) = 20; Rcpp::Rcout << "Armadill Matrix after modification: " << std::endl << arma_B << std::endl; Rcpp::Rcout << "Eigen Matrix after modification: " << std::endl << eigen_A << std::endl; }
跑:
map_update(x)
输出:
Eigen Matrix on Entry: 1 3 2 4 Armadill Matrix after modification: 10.0000 3.0000 2.0000 20.0000 Eigen Matrix after modification: 10 3 2 20
Using armadillo's
.memptr()
class member function, we are able to extract the memory pointer. From here, we can use Eigen'sMap<T>()
constructor to create an Eigen matrix.Now, we can go from the Eigen matrix using the
.data()
member function to extract a point to Eigen's memory structure. Then, using the advanced constructor options ofarma::mat
we can create an armadillo matrix.For example:
#include <RcppArmadillo.h> #include <RcppEigen.h> // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] Eigen::MatrixXd example_cast_eigen(arma::mat arma_A) { Eigen::MatrixXd eigen_B = Eigen::Map<Eigen::MatrixXd>(arma_A.memptr(), arma_A.n_rows, arma_A.n_cols); return eigen_B; } // [[Rcpp::export]] arma::mat example_cast_arma(Eigen::MatrixXd eigen_A) { arma::mat arma_B = arma::mat(eigen_A.data(), eigen_A.rows(), eigen_A.cols(), false, false); return arma_B; } /***R (x = matrix(1:4, ncol = 2)) example_cast_eigen(x) example_cast_arma(x) */
Results:
(x = matrix(1:4, ncol = 2)) # [,1] [,2] # [1,] 1 3 # [2,] 2 4 example_cast_eigen(x) # [,1] [,2] # [1,] 1 3 # [2,] 2 4 example_cast_arma(x) # [,1] [,2] # [1,] 1 3 # [2,] 2 4
One quick remark: If you are using Eigen's Mapping function, then you should automatically have the change in the Armadillo matrix (and vice versa), e.g.
#include <RcppArmadillo.h> #include <RcppEigen.h> // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] void map_update(Eigen::MatrixXd eigen_A) { Rcpp::Rcout << "Eigen Matrix on Entry: " << std::endl << eigen_A << std::endl; arma::mat arma_B = arma::mat(eigen_A.data(), eigen_A.rows(), eigen_A.cols(), false, false); arma_B(0, 0) = 10; arma_B(1, 1) = 20; Rcpp::Rcout << "Armadill Matrix after modification: " << std::endl << arma_B << std::endl; Rcpp::Rcout << "Eigen Matrix after modification: " << std::endl << eigen_A << std::endl; }
Run:
map_update(x)
Output:
Eigen Matrix on Entry: 1 3 2 4 Armadill Matrix after modification: 10.0000 3.0000 2.0000 20.0000 Eigen Matrix after modification: 10 3 2 20
相关问答
更多-
NSString到NSData : NSString* str= @"teststring"; NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding]; NSData到NSString : NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; ...
-
如何转换Eigen :: Affine3f至Eigen :: Matrix4f(How to convert from Eigen::Affine3f to Eigen::Matrix4f)[2023-01-09]
是的,在内部,Affine3f存储了MatrixXf,因此您可以: Eigen::Affine3f A; Eigen::Matrix4f M; M = A.matrix(); A = M; // assume that M.row(3) == [0 0 0 1] Yes, internally an Affine3f stores a MatrixXf, so you can do: Eigen::Affine3f A; Eigen::Matrix4f M; M = A.mat ... -
Si! 特征矩阵可以作为参考传递。 这是一个工作示例: #include
#include void calc(Eigen::MatrixXf& mat) { mat = Eigen::MatrixXf::Constant(mat.rows(), mat.cols(), 1.0); } int main() { Eigen::MatrixXf Mat; Mat = Eigen::Matrix4f::Identity(); ... -
这是不受支持的,因为严格地说,你会为任何显式零计算0/0。 如果矩阵处于压缩模式,您可以解决方法,以确保调用: beta.makeCompressed(); 然后将非零值映射为密集数组: Map
a(beta.valuePtr(), beta.nonZeros(); (a / a.abs()).sum; This is not supported because rigorously you would compute 0/0 for any explicit zeros. You c ... -
犰狳整数特征分解(Armadillo integer eigen decomposition)[2023-09-06]
首先使用conv_to函数将整数矩阵转换为双矩阵。 例如, imat A = ...; mat B = conv_to::from(A); imat A = ...; mat B = conv_to ::from(A); 。 然后,您可以对转换后的矩阵进行特征分解。 First convert the integer matrix to a double matrix using the conv_to function. For example, imat A = ...; mat B ... -
如果您的矩阵无法使用,并且您想要将所有1都翻转为0 ... mat <- 1-mat If your matrix is mat and you want to flip all 1s as 0s... mat <- 1-mat
-
对于您可能希望尽可能快的事情,您可能需要考虑编写自己的循环以将short[]数组复制到新的int[]数组中,例如: public static int[] Convert(short[] input) { int[] result = new int[input.Length]; for (int i = 0; i < input.Length; ++i) result[i] = input[i]; return result; } 这是你如何转换回来。 重要提 ...
-
如何将邻接矩阵转换为发生率矩阵,反之亦然?(How do you transform Adjacency matrices to Incidence matrices and vice-versa?)[2021-12-14]
您可以通过查看顶点之间的每个可能的连接将邻接矩阵转换为关联矩阵,并且只要确实存在连接,就为您的关联矩阵添加边。 但是,要小心只查看每个顶点组合一次。 反过来说,你可以简单地看一下每条边。 入射矩阵为每条边指定了它连接的两个顶点。 您无法重新创建的一种情况是,当多个边缘连接相同的两个顶点时。 这里有一些源代码来说明上面的解释( 见工作 ): #include#include #include typedef std::vector m ... -
“这很复杂。” 我们通过附加软件包RcppArmadillo和RcppEigen为Armadillo和Eigen提供绑定,因此比较和赛马问题出现了很多。 而且我认为没有一个明确的答案。 为了使事情“更糟”,犰狳通常指你安装的任何LAPACK / BLAS,因此你可以使用多核并行,而Eigen倾向于选择自己的例程。 在准备我的Rcpp书籍时,我做了一些时间安排,发现了一些反直觉的结果。 在一天结束时,您可能需要简单描述您的问题。 "It's complicated." We offer bindings fo ...
-
将犰狳矩阵转换为特征MatriXd,反之亦然(Converting an Armadillo Matrix to an Eigen MatriXd and vice versa)[2022-12-22]
使用armadillo的.memptr()类成员函数,我们可以提取内存指针。 从这里,我们可以使用Eigen的Map()构造函数来创建一个特征矩阵。 现在,我们可以使用.data()成员函数从Eigen矩阵中提取一个指向Eigen内存结构的点。 然后,使用arma::mat的高级构造函数选项,我们可以创建一个armadillo 矩阵 。 例如: #include #include // [[Rcpp::depends(RcppEigen ...