Пример #1
0
  std::vector<std::vector<int>> matrixReshape(const std::vector<std::vector<int>>& nums, const unsigned int &r, const unsigned int &c)
  {
    if (r * c != nums.size() * nums[0].size())
    {
      return nums;
    }

    std::vector<std::vector<int>> reshaped_matrix(r, std::vector<int>(c));
    size_t reshaped_row = 0;
    size_t reshaped_col = 0;
    for (size_t row = 0; row < nums.size(); row++)
    {
      for (size_t col = 0; col < nums[0].size(); col++)
      {
        reshaped_matrix[reshaped_row][reshaped_col] = nums[row][col];
        reshaped_col++;
        if (reshaped_col == c)
        {
          reshaped_col = 0;
          reshaped_row++;
        }
      }
    }
    return reshaped_matrix;
  }
Пример #2
0
//Reshape the vector into a matrix : Similar to MATLAB
vnl_matrix<double> MCLR_SM::Reshape_Vector(vnl_vector<double>vec,int r,int c )
{
	if(vec.size() != r*c)
	{
		std::cerr << "Number of elements in the matrix/vector should be equal to the total number of elements in the reshaped matrix";
		exit(1);
	}
	
	vnl_matrix<double>reshaped_matrix;
	reshaped_matrix.set_size(r,c);
	int count = 0;
	
	for(int j=0;j<c;++j)	
	{
		for(int i=0;i<r;++i)
		{
			reshaped_matrix(i,j) = vec(count);
			count++;
		}
	}
	return reshaped_matrix;
}
Пример #3
0
//Reshape the matrix : columns first ; Similar to MATLAB
vnl_matrix<double> MCLR_SM::Reshape_Matrix(vnl_matrix<double>mat,int r,int c )
{
	if(mat.rows()*mat.cols() != r*c)
	{
		cout<< "Number of elements in the matrix/vector should be equal to the total number of elements in the reshaped matrix";
		getchar();
		exit(1);
	}
	
	vnl_matrix<double>reshaped_matrix;
	reshaped_matrix.set_size(r,c);
	int count = 0;
	
	for(int j=0;j<c;++j)	
	{
		for(int i=0;i<r;++i)
		{
			reshaped_matrix(i,j) = mat(count%mat.rows(),floor(static_cast<double>(count/mat.rows())));
			count++;
		}
	}
	return reshaped_matrix;
}