创建于MEX稀疏矩阵 [英] Creating sparse matrix in MEX

查看:556
本文介绍了创建于MEX稀疏矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建用C编写的创建矩阵如何访问元素单独类似于C经过MEX文件二维稀疏矩阵,说垫[I] [J]

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]?

我厌倦了使用 mxCreateNumericArray 功能,但我不能够访问的元素,也使它作为一个稀疏矩阵。

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

请帮忙

推荐答案

参见mxCreateSparse这个网页。然后你就会想看看 mxSetPr ,的mxSetIr 并的 mxSetJc 和相应的获取的版本。

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

<一个href=\"http://web.archive.org/web/20060211061327/http://amath.colorado.edu/computing/Matlab/OldTechDocs/apiref/mxcreatesparse.html\">Here's如何分配一个稀疏矩阵的例子。的我意识到这是一个古老的联系,但据我所知,它并没有改变。

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.

基本上,它是如何工作是, IR 数据包含的行索引。在 JR 数据包含指数列表中的 IR 阵列。例如,在如何分配稀疏矩阵的环节,code:

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};
...

阵列 static_jc_data 告诉你指数 static_jc_data [C] static_jc_data [C +1] -1 static_pr_data static_ir_data 对应列<$ C $ ç> C 矩阵。在此范围内( static_jc_data [C] static_jc_data [C + 1] -1 )<$ C的条目$ C> static_pr_data 为您提供了矩阵的值和 static_ir_data 为您提供了正确的行。

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.

例如,这里的矩阵是:

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] 来看看是否你的 I 存在。如果确实如此,那么在 static_pr_data 相应的条目将包含您的输入。如果没有,则返回0。

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.

不过,通常与稀疏矩阵的使用情况,如果你做了很多通过矩阵搜索来查看某个元素存在,你可能要想想你如何使用它。通常情况下,这是更好的执行你通过仅在非零元素去一次,而不是搜索的每个 I,J 个入口做什么操作。

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.

哦,还有最后一件事。请记住,在MEX code,所有的指标是基于0的,但它们是基于MATLAB中1。这应该助兴。

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.

这篇关于创建于MEX稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆