得到由向量的向量表示的矩阵的第一列
Get the first column of a matrix represented by a vector of vectors
假设我用std::vector
表示值的矩阵foo
:
int rows = 5;
int cols = 10;
auto foo = vector<vector<double>>(rows, vector<double>(cols));
是否有一个聪明的简单的方法让我得到一个vector<int>
的大小rows
,包含foo的第一个"列":
{foo[0][0], foo[0][1], foo[0][2], foo[0][3], foo[0][4] }
换句话说,我能否"调换"foo,使以下三件事为真呢?
foo_transpose.size() == cols
foo_transpose[0].size() == rows
foo_transpose[0] == {foo[0][0], foo[0][1], foo[0][2], foo[0][3], foo[0][4] }
澄清注意
对于表示"矩阵"的替代方法有一些很好的建议。当我使用术语"矩阵"时,我只是指每个二级vector
的大小相同。我并不是说我将使用这种数据结构来进行线性代数类型的运算。实际上,我确实需要一个向量的向量,或者一个可以从中"拉出"1D向量的数据结构,因为我有一些对向量进行操作的函数,比如:
double sum(vector<double> const & v);
我用:
sum(foo[0]);
只是在一个特殊的情况下,我遇到了一个需要做的事情:
sum({foo[0][0], foo[0][1], foo[0][2], foo[0][3], foo[0][4] };
For Loop Solution
有一个明显的for循环解决方案,但我在寻找更健壮和高效的东西。
正如我在评论中提到的,由于以下几个原因,使用向量的向量表示矩阵是不实际的:
- 设置起来很繁琐;
- 难以改变;
- 缓存位置错误。
这是我创建的一个非常简单的类,它将在单个向量中保存2D矩阵。像MATLAB这样的软件就是这么做的……虽然是一个巨大的简化。
template <class T>
class SimpleMatrix
{
public:
SimpleMatrix( int rows, int cols, const T& initVal = T() );
// Size and structure
int NumRows() const { return m_rows; }
int NumColumns() const { return m_cols; }
int NumElements() const { return m_data.size(); }
// Direct vector access and indexing
operator const vector<T>& () const { return m_data; }
int Index( int row, int col ) const { return row * m_cols + col; }
// Get a single value
T & Value( int row, int col ) { return m_data[Index(row,col)]; }
const T & Value( int row, int col ) const { return m_data[Index(row,col)]; }
T & operator[]( size_t idx ) { return m_data[idx]; }
const T & operator[]( size_t idx ) const { return m_data[idx]; }
// Simple row or column slices
vector<T> Row( int row, int colBegin = 0, int colEnd = -1 ) const;
vector<T> Column( int col, int rowBegin = 0, int rowEnd = -1 ) const;
private:
vector<T> StridedSlice( int start, int length, int stride ) const;
int m_rows;
int m_cols;
vector<T> m_data;
};
这个类基本上是围绕一个函数StridedSlice
进行糖衣修饰。它的实现是:
template <class T>
vector<T> SimpleMatrix<T>::StridedSlice( int start, int length, int stride ) const
{
vector<T> result;
result.reserve( length );
const T *pos = &m_data[start];
for( int i = 0; i < length; i++ ) {
result.push_back(*pos);
pos += stride;
}
return result;
}
其余部分非常简单:
template <class T>
SimpleMatrix<T>::SimpleMatrix( int rows, int cols, const T& initVal )
: m_data( rows * cols, initVal )
, m_rows( rows )
, m_cols( cols )
{
}
template <class T>
vector<T> SimpleMatrix<T>::Row( int row, int colBegin, int colEnd ) const
{
if( colEnd < 0 ) colEnd = m_cols-1;
if( colBegin <= colEnd )
return StridedSlice( Index(row,colBegin), colEnd-colBegin+1, 1 );
else
return StridedSlice( Index(row,colBegin), colBegin-colEnd+1, -1 );
}
template <class T>
vector<T> SimpleMatrix<T>::Column( int col, int rowBegin, int rowEnd ) const
{
if( rowEnd < 0 ) rowEnd = m_rows-1;
if( rowBegin <= rowEnd )
return StridedSlice( Index(rowBegin,col), rowEnd-rowBegin+1, m_cols );
else
return StridedSlice( Index(rowBegin,col), rowBegin-rowEnd+1, -m_cols );
}
请注意,Row
和Column
函数的设置方式使您可以轻松地请求整个行或列,但是功能更强大,因为您可以通过传递一个或两个参数来分割范围。是的,您可以通过使起始值大于结束值来反向返回行/列。
这些函数中没有内置边界检查,但是您可以很容易地添加它。
你也可以添加一些东西来返回一个区域切片作为另一个SimpleMatrix<T>
。
玩。
相关文章:
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 如何在C++中获取二维数组中最少的一列数?
- 如何允许用户选择何时停止三列中的每一列的旋转
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- 在犰狳立方体中添加一列 1 的有效方法
- 在二维向量的每一列中查找最大值
- 只有一列适合其内容大小,而另一列则拉伸
- 如何将向量一的所有元素与向量二进行比较,如果存在最大元素,则将向量二的所有元素与向量三进行比较?
- 如何将单独的数组分组在一起(类似于按一列对表进行排序)
- C++ 最后一列中的直方图错误
- 添加两个矩阵会打印一列垃圾数据 c++
- C 特征:通过参考将矩阵的一列传递到函数中
- 仅制作一列qtreewidget可编辑//故障排除
- 在 QTableView 中仅对一列进行排序,而不对其他列进行排序.独立列
- 按列对CSV文件进行排序,并与C 中的另一列进行比较
- 隔离C 中向量的一列
- 读取数据文件并将每一列分配给各个数组
- 按一列对二维数组进行气泡排序
- C++ 向量 下一列的向量大小
- 访问矩阵的一列作为特征中的向量