EIGEN:一次性复制多个行列数据

实现类比于MATLAB的行列操作

即:提取指定行列,或删除指定行列
A(:,index1:index2);或A(:,[index1,index2,...,indexn]);
A(index1:index2,:);或A([index1,index2,...,indexn],:);
A(:,index1:index2)=[];或A(:,[index1,index2,...,indexn])=[];
A(index1:index2,:)=[]或A([index1,index2,...,indexn],:)=[];
#include
#include
#include
using namespace std;
using namespace Eigen;

class Extra_Del
{
public:
	MatrixXd rows_ext_V(VectorXi ind, MatrixXd matrix);
	MatrixXd rows_ext_M(MatrixXd ind, MatrixXd matrix);
	MatrixXd cols_ext_V(VectorXi ind, MatrixXd matrix);
	MatrixXd cols_ext_M(MatrixXd ind, MatrixXd matrix);
	MatrixXd rows_del_V(VectorXi ind, MatrixXd matrix);
	MatrixXd rows_del_M(MatrixXd ind, MatrixXd matrix);
	MatrixXd cols_del_V(VectorXi ind, MatrixXd matrix);
	MatrixXd cols_del_M(MatrixXd ind, MatrixXd matrix);
};
MatrixXd Extra_Del::rows_ext_V(VectorXi ind, MatrixXd matrix){//行提取**********
	MatrixXd zs1(ind.size(), 1);
	zs1 << (ind.head(ind.size())).cast();
	MatrixXd final_matrix(zs1.size(), matrix.cols());
	int num = zs1.size();
	for (int k = 0; k < num; k++)
	{
		final_matrix.row(k) = matrix.row(zs1(k, 0));
	}
	return final_matrix;
}
MatrixXd Extra_Del::rows_ext_M(MatrixXd ind, MatrixXd matrix){//行提取**********
	MatrixXd final_matrix(ind.size(), matrix.cols());
	int num = ind.size();
	for (int k = 0; k < num; k++)
	{
		final_matrix.row(k) = matrix.row(ind(k,0));
	}
	return final_matrix;
}
MatrixXd Extra_Del::rows_del_V(VectorXi ind, MatrixXd matrix){//行取反删除**********
	double num=matrix.rows();
	VectorXd xl(num);
	for (int i = 0; i < num; i++)
	{
		xl(i) = i;
	}
	for (int i = 0; i < ind.size(); i++)
	{
		xl(ind(i)) = NAN;
	}
	VectorXd out_index(num - ind.size());
	int index(0);
	for (int i = 0; i < num; i++){
		if (isnan(xl(i)))
		{
			continue;
		}
		else
		{
			out_index(index) = i;
		}
		index++;
	}
	MatrixXd zs1(out_index.size(), 1);
	zs1 << (out_index.head(out_index.size())).cast();
	MatrixXd final_matrix(zs1.size(), matrix.cols());
	int num1 = zs1.size();
	for (int k = 0; k < num1; k++)
	{
		final_matrix.row(k) = matrix.row(zs1(k, 0));
	}
	return final_matrix;
}
MatrixXd Extra_Del::rows_del_M(MatrixXd ind, MatrixXd matrix){//行取反删除**********
	double num = matrix.rows();
	VectorXd xl(num);
	for (int i = 0; i < num; i++)
	{
		xl(i) = i;
	}
	for (int i = 0; i < ind.size(); i++)
	{
		xl(ind(i)) = NAN;
	}
	VectorXd out_index(num - ind.size());
	int index(0);
	for (int i = 0; i < num; i++){
		if (isnan(xl(i)))
		{
			continue;
		}
		else
		{
			out_index(index) = i;
		}
		index++;
	}
	MatrixXd zs1(out_index.size(), 1);
	zs1 << (out_index.head(out_index.size())).cast();
	MatrixXd final_matrix(zs1.size(), matrix.cols());
	int num1 = zs1.size();
	for (int k = 0; k < num1; k++)
	{
		final_matrix.row(k) = matrix.row(zs1(k, 0));
	}
	return final_matrix;
}
MatrixXd Extra_Del::cols_ext_V(VectorXi ind, MatrixXd matrix){//列提取*********
	MatrixXd zs1(ind.size(), 1);
	zs1 << (ind.head(ind.size())).cast();
	MatrixXd final_matrix(matrix.rows(), zs1.size());
	int num = zs1.size();
	for (int k = 0; k < num; k++)
	{
		final_matrix.col(k) = matrix.col(zs1(k, 0));
	}
	return final_matrix;
}
MatrixXd Extra_Del::cols_ext_M(MatrixXd ind, MatrixXd matrix){//列提取*********
	MatrixXd final_matrix(matrix.rows(), ind.size());
	int num = ind.size();
	for (int k = 0; k < num; k++)
	{
		final_matrix.col(k) = matrix.col(ind(k, 0));
	}
	return final_matrix;
}
MatrixXd Extra_Del::cols_del_V(VectorXi ind, MatrixXd matrix){//行取反删除**********
	double num = matrix.rows();
	VectorXd xl(num);
	for (int i = 0; i < num; i++)
	{
		xl(i) = i;
	}
	for (int i = 0; i < ind.size(); i++)
	{
		xl(ind(i)) = NAN;
	}
	VectorXd out_index(num - ind.size());
	int index(0);
	for (int i = 0; i < num; i++){
		if (isnan(xl(i)))
		{
			continue;
		}
		else
		{
			out_index(index) = i;
		}
		index++;
	}
	MatrixXd zs1(out_index.size(), 1);
	zs1 << (out_index.head(out_index.size())).cast();
	MatrixXd final_matrix(matrix.rows(), zs1.size());
	int num1 = zs1.size();
	for (int k = 0; k < num1; k++)
	{
		final_matrix.col(k) = matrix.col(zs1(k, 0));
	}
	return final_matrix;
}
MatrixXd Extra_Del::cols_del_M(MatrixXd ind, MatrixXd matrix){//行取反删除**********
	double num = matrix.rows();
	VectorXd xl(num);
	for (int i = 0; i < num; i++)
	{
		xl(i) = i;
	}
	for (int i = 0; i < ind.size(); i++)
	{
		xl(ind(i)) = NAN;
	}
	VectorXd out_index(num - ind.size());
	int index(0);
	for (int i = 0; i < num; i++){
		if (isnan(xl(i)))
		{
			continue;
		}
		else
		{
			out_index(index) = i;
		}
		index++;
	}
	MatrixXd zs1(out_index.size(), 1);
	zs1 << (out_index.head(out_index.size())).cast();
	MatrixXd final_matrix(matrix.rows(), zs1.size());
	int num1 = zs1.size();
	for (int k = 0; k < num1; k++)
	{
		final_matrix.col(k) = matrix.col(zs1(k, 0));
	}
	return final_matrix;
}

主函数:

#include
#include
#include
#include"Extra_Del.h"
using namespace std;
using namespace Eigen;

int main(){
	MatrixXd ind(3,1);
	Matrix matrix;
	matrix << 1, 2, 3, 4, 5,
		4, 5, 6, 7, 8,
		7, 5, 4, 5, 2,
		1, 4, 7, 8, 5,
		1, 2, 3, 6, 5;
	ind << 1, 2, 3;
	MatrixXd bb;
	Extra_Del aa;
	cout << "原矩阵-------------------------------" << endl;
	cout << "提取的索引为" << ind(0) << " " << ind(1) << " " << ind(2) << "-----"<

运行结果:
EIGEN:一次性复制多个行列数据_第1张图片

你可能感兴趣的:(eigen)