索引和访问 mex 函数中单元数组和矩阵的元素
Indexing and accessing elements of cell arrays and matrices in mex functions
我正在尝试编写一个 C++ mex 函数来迭代矩阵的单元格数组,每个矩阵的大小都不同。在 Matlab 中,我可以使用以下代码来执行此操作:
function Z = myFunction(X, Z, B)
for i = 1:size(X, 1)
for j = 1:size(X, 2)
for k = 1:size(X, 3)
temp = X{i, j, k};
for m = 1:size(temp, 1)
Z{temp(m, 1)}(temp(m, 2)) = Z{temp(m, 1)}(temp(m, 2)) + B(i, j, k);
end
end
end
end
这里 X 是一个三维单元格数组,其中每个单元格包含一个具有可变行数和 2 列的矩阵。这两列允许我索引向量 Z 的另一个单元格数组,其中每个向量具有不同的长度。Z 中被索引的向量元素由三维矩阵 B 中的元素递增。
到目前为止,我在C++中有以下代码(我以前从未用C++编码过(:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const mxArray* X = prhs[0];
const mxArray* Z = prhs[1];
const mxArray* B = prhs[2];
const int* pDims = mxGetDimensions(X);
mwSize nsubs = mxGetNumberOfDimensions(X);
for (size_t i = 0; i < pDims[0]; i++) {
for (size_t j = 0; j < pDims[1]; j++) {
for (size_t k = 0; k < pDims[2]; k++) {
int subs [] = {i, j, k};
mxArray* temp = mxGetCell(X, mxCalcSingleSubscript(X, nsubs, subs));
const int* matDims = mxGetDimensions(temp);
for (size_t m = 0; m < matDims[0]; m++) {
}
}
}
}
}
问题:
- 要访问矩阵 B 的元素,我是否可以使用与访问单元格数组 X 的元素相同的 mxCalcSingleSubscript 函数?如果没有,我该怎么做?
- 如何访问
temp
元素并像在 Matlab 代码中所做的那样执行索引?
-由于所有输入数组都是const
因此您应该复制Z
。
-mxCalcSingleSubscript
可用于任何类型的数组,包括单元数组。在这里,我将其重命名为sub2ind
。
-mxGetPr
用于访问数组的元素。
这是一个实现(尚未在真实数据上进行测试(:
#include "mex.h"
#define sub2ind mxCalcSingleSubscript
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const mxArray* X = prhs[0];
const mxArray* Z = prhs[1];
const mxArray* B = prhs[2];
mxArray * out = mxDuplicateArray (Z);
const int* pDims = mxGetDimensions(X);
mwSize nsubs = mxGetNumberOfDimensions(X);
double* B_arr = mxGetPr(B);
for (size_t i = 0; i < pDims[0]; i++) {
for (size_t j = 0; j < pDims[1]; j++) {
for (size_t k = 0; k < pDims[2]; k++) {
int subs [] = {i, j, k};
mwIndex idx = sub2ind(X, nsubs, subs);
mxArray* temp = mxGetCell(X, idx);
double* temp_arr = mxGetPr(temp);
const int* matDims = mxGetDimensions(temp);
mwSize nsubs_temp = mxGetNumberOfDimensions(temp);
for (size_t m = 0; m < matDims[0]; m++) {
int subs_out_1 [] = {m,0};
int subs_out_2 [] = {m,1};
mwIndex temp_m_1 = temp_arr[sub2ind(temp, nsubs_temp, subs_out_1)]-1;
mwIndex temp_m_2 = temp_arr[sub2ind(temp, nsubs_temp, subs_out_2)]-1;
double* Z_out = mxGetPr (mxGetCell(out,temp_m_1));
Z_out[temp_m_2] += B_arr[idx];
}
}
}
}
plhs[0] = out;
}
但是,MATLAB 和 c 实现都可以更改为使用线性索引:
function Z = myFunction(X, Z, B)
for k = 1:numel(X)
for m = 1:size(X{k}, 1)
Z{X{k}(m, 1)}(X{k}(m, 2)) = Z{X{k}(m, 1)}(X{k}(m, 2)) + B(k);
end
end
end
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const mxArray* X = prhs[0];
const mxArray* Z = prhs[1];
const mxArray* B = prhs[2];
mxArray * out = mxDuplicateArray (Z);
mwSize n_X = mxGetNumberOfElements(X);
double* B_arr = mxGetPr(B);
for (size_t k = 0; k < n_X; k++) {
mxArray* temp = mxGetCell(X, k);
double* temp_arr = mxGetPr(temp);
const int* matDims = mxGetDimensions(temp);
size_t rows = matDims[0];
for (size_t m = 0; m < rows; m++) {
mwIndex temp_m_1 = temp_arr[m]-1;
mwIndex temp_m_2 = temp_arr[m+rows]-1;
double* Z_out = mxGetPr (mxGetCell(out,temp_m_1));
Z_out[temp_m_2] += B_arr[k];
}
}
plhs[0] = out;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 如何将元素添加到数组的线程安全函数?
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 输出没有重复元素的动态数组(收缩数组)C++
- 数组中最大的非重复元素
- 数组元素打印的递归方法
- 对字符数组中的元素执行逐位操作
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 通过交换元素使数组相同
- 按平均值替换数组中的元素
- 打印矢量数组中的所有元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 如何将元素从向量转移到新数组?
- 如何在向量数组中插入元素?
- 为什么 2 个相同数组的元素彼此相等
- C++ 中的二维整数数组,每行中的元素数量不均匀