在 MEX 中创建稀疏矩阵

发布于 2024-11-09 14:23:28 字数 266 浏览 0 评论 0原文

如何在用 C 编写的 MEX 文件中创建二维稀疏矩阵。创建矩阵后如何像 C 中那样单独访问元素,比如 mat[i][j]

我厌倦了使用 mxCreateNumericArray 函数,但我不是无法访问元素并将其设置为稀疏矩阵。

请帮忙

How to create a 2d sparse matrix in a MEX-file written in C. After creating the matrix how to access the elements individually like in C , say mat[i][j]?

I tired using mxCreateNumericArray function but I wasn't able to access the elements and also make it as a sparse matrix.

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2024-11-16 14:23:28

查看 mxCreateSparse 上的此页面。 然后您将需要查看 < a href="http://www.mathworks.com/help/matlab/apiref/mxsetpr.html" rel="noreferrer">mxSetPr, mxSetIrmxSetJc 和相应的“get”版本。

这是一个示例如何分配稀疏矩阵。我意识到这是一个旧链接,但据我所知,它没有改变。

基本上,它的工作原理ir 数据包含行索引。 jr 数据包含 ir 数组的索引列表。例如,在如何分配稀疏矩阵的链接中,代码:

...
static double  static_pr_data[NZMAX] = {5.8, 6.2, 5.9, 6.1};
static int     static_ir_data[NZMAX] = {0, 2, 1, 3};
static int     static_jc_data[COLS+1] = {0, 2, 4};
...

数组 static_jc_data 告诉您索引 static_jc_data[c]static_jc_data[c+ static_pr_datastatic_ir_data的1]-1对应于矩阵的c列。在该范围内(static_jc_data[c]static_jc_data[c+1]-1),static_pr_data 的条目为您提供矩阵中的值static_ir_data 为您提供正确的行。

例如,这里的矩阵为:

A = [ 5.8  0
      0    5.9
      6.2  0
      0    6.1];

要回答有关如何单独访问元素的问题,您必须搜索第 i,j 个元素是否存在,如果存在则返回该元素,否则返回 0为此,您需要从 static_ir_data[static_jc_data[j]]static_ir_data[static_jc_data[j+1]-1] 进行搜索,以查看您的 我存在。如果是,则 static_pr_data 中的相应条目将包含您的条目。如果不存在,则返回 0。

但是,通常在使用稀疏矩阵时,如果您要对矩阵进行大量搜索以查看某个元素是否存在,您可能需要考虑如何使用它。通常,执行您正在执行的任何操作时,最好只遍历一次非零元素,而不是搜索每个第 i,j 个条目。

哦,还有最后一件事。请记住,在 MEX 代码中,所有索引都是基于 0 的,但在 MATLAB 中它们都是基于 1 的。这应该会增加乐趣。

See this page on mxCreateSparse. Then you'll want to look at mxSetPr, mxSetIr and mxSetJc and the corresponding "get" versions.

Here's an example of how to allocate a sparse matrix. I realize this is an old link, but to the best of my knowledge, it hasn't changed.

Basically, how it works is that the ir data contains the row indices. The jr data contains a list of indices into the ir array. For instance, in the link on how to allocate a sparse matrix, the code:

...
static double  static_pr_data[NZMAX] = {5.8, 6.2, 5.9, 6.1};
static int     static_ir_data[NZMAX] = {0, 2, 1, 3};
static int     static_jc_data[COLS+1] = {0, 2, 4};
...

the array static_jc_data tells you that indices static_jc_data[c] through static_jc_data[c+1]-1 of static_pr_data and static_ir_data correspond to the column c of the matrix. Within that range (static_jc_data[c] to static_jc_data[c+1]-1) the entries of static_pr_data gives you the values in the matrix and static_ir_data gives you the correct rows.

For example, the matrix here would be:

A = [ 5.8  0
      0    5.9
      6.2  0
      0    6.1];

To answer your questions about how to access elements individually, you have to search for whether the i,jth element exists and if it does return it, otherwise return 0. To do this, you'd search from static_ir_data[static_jc_data[j]] through static_ir_data[static_jc_data[j+1]-1] to see whether your i exists. If it does, then the corresponding entry in static_pr_data will contain your entry. If it doesn't, then return 0.

However, typically with sparse matrix usage, if you're doing a lot of searching through the matrix to see if a certain element exists, you may want to think about how you're using it. Typically, it's much better to perform whatever operation you're doing by only going through the non-zero elements once instead of searching for each i,jth entry.

Oh, and one last thing. Keep in mind that in the MEX code, all your indices are 0 based, but they are 1 based in MATLAB. That should add to the fun.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文