c++ - 创建一个可以返回对角矩阵的 C++ mex 函数
问题描述
我需要创建一个 c++ mex 函数,该函数输入一个矩阵并将其与每个数组在其自己的块中的对角线一起返回。前任。
input_matrix = ([1,2,3;4,5,6;7,8,9])
output_matrix = 1 2 3 0 0 0 0 0 0
0 0 0 4 5 6 0 0 0
0 0 0 0 0 0 7 8 9
谁能帮助我在这一点上非常迷茫。
解决方案
我为你做了这个例子,基本的构建块是 std::vector。矩阵可以建模为 std::vector 的 std::vector。std::vector 是动态分配数组的 C++ 标准库类型
#include <array>
#include <cassert>
#include <vector>
#include <iostream>
// for fixed size arrays use templates, this will also ensure contiguous memory allocation
// to be compatible with other libraries
template<typename type_t, std::size_t rows_v, std::size_t cols_v>
auto create_diagonal_matrix(const type_t(&input)[rows_v][cols_v])
{
// unlike "C" style arrays std::array can be returned from a function
std::array<std::array<type_t,cols_v*rows_v>, rows_v> matrix;
std::size_t prefix_size = 0ul; // number of zeros in front of the next row
std::size_t postfix_size = (rows_v - 1ul) * cols_v; // how many zeros to append at the end of next row;
for (std::size_t row = 0ul; row < rows_v; ++row)
{
std::size_t col = 0ul;
// fill prefix_size indices with 0
for (std::size_t n = 0ul; n < prefix_size; ++n, ++col) matrix[row][col] = 0;
// fill input row size entries with values from the input
for (std::size_t n = 0ul; n < rows_v; ++n, ++col) matrix[row][col] = input[row][n];
// then fill postfix_size indices with 0
for (std::size_t n = 0ul; n < postfix_size; ++n, ++col) matrix[row][col] = 0;
// adjuest the prefix and postfix sizes
prefix_size += rows_v;
postfix_size -= rows_v;
}
return matrix;
}
auto create_diagonal_matrix_dynamic(const std::vector<std::vector<int>>& input)
{
std::vector<std::vector<int>> matrix;
assert(input.size() > 0);
auto row_size = input[0].size();
std::size_t prefix_size = 0ul; // number of zeros in front of the next row
std::size_t postfix_size = (input.size() - 1ul) * row_size; // how many zeros to append at the end of next row;
for (const auto& row : input)
{
assert(row.size() == row_size); // runtime validation, all rows must have same length
std::vector<int> new_row;
// fill prefix_size indices with 0
for (std::size_t n = 0ul; n < prefix_size; ++n) new_row.push_back(0);
// fill input row size entries with values from the input
for (std::size_t n = 0ul; n < row_size; ++n) new_row.push_back(row[n]);
// then fill postfix_size indices with 0
for (std::size_t n = 0ul; n < postfix_size; ++n) new_row.push_back(0);
matrix.push_back(new_row);
// adjuest the prefix and postfix sizes
prefix_size += row_size;
postfix_size -= row_size;
}
return matrix;
}
int main()
{
// use an initializer list to setup the 2D input array
// I think in your code this should be done through parsing an input string
// and then build up a std::vector<std::vector<int>> yourself.
auto matrix = create_diagonal_matrix({ {1,2,3}, {4,5,6}, {7,8,9} });
// output the generated matrix.
for (const auto& row : matrix)
{
bool comma = false;
for (const auto& value : row)
{
if (comma) std::cout << ", ";
std::cout << value;
comma = true;
}
std::cout << "\n";
}
std::cout << std::endl;
// to show memory is contiguous in std::array<std::array<int,9>,3>
// this is how to get access to contiguous memory for std::array
auto data_ptr = matrix.data();
for (std::size_t row = 0; row < matrix.size(); row++)
{
bool comma = false;
for (std::size_t col = 0; col < matrix[0].size(); col++)
{
if (comma) std::cout << ", ";
std::cout << data_ptr[row][col];
comma = true;
}
std::cout << "\n";
}
// creating a mex file is desribed here
// https://www.mathworks.com/help/matlab/matlab_external/c-mex-source-file.html
return 0;
}
推荐阅读
- r - 任何人都知道为什么我的 SankeyNetwork 没有绘制图表(显然没有错误)?
- javascript - 获取符合条件的对象数组中的第一个对象
- flutter - Flutter:传递到下一个屏幕的变量变为null
- google-apps-script - Google 表格 SQL 查询超时
- c++ - 将 arrayfire 与 boost::odeint 耦合
- bash - 比较行是否包含短语(带有 /n 或多行的正则表达式?)
- android - 如何在 Pjsip 上设置端口
- c++ - 向量的最大尺寸是多少?
- c# - String.Split 似乎没有正确使用重载
- elasticsearch - 我们可以从 Kubernetes pod 访问节点/主机进程吗?