Sparse matrix construction in simulink
Show older comments
Hi,
Background: In a simulink model I want to construct and solve a linear system. My matrix is sparse and I want to exploit this property. Hence I want to construct a sparse matrix and solve it. I want to do this without calling an extrinsic matlab function, because this slows down the simulation and, also, I need to use the solution of the linear system back in my simulation. My approach is to create a c/c++ function that does the job and then mex it. This way I hope to use it in my simulink model like any other library block.
Current situation: I already have two m functions that construct the matrix and solves it respectively. I just need to recreate this function in c/c++. In those functions, however, I use cell arrays and sparse matrix operations. Therefore I cannot use Matlab coder to auto-generate code for me. I guess I need to it the hard way by making everything in c/c++!
My problem: I am not very familiar with c/c++. I have also never created any "mexable" c/c++ code. Right now I am trying to build small snippets of my code in c and then compile it. I want to create the following c-version of this function:
[p] = test(Fx,c)
p = cell(size(Fx,1),1);
for i=1:size(Fx,1);
p{i} = Fx{i}*c;
end
NOTE: Fx is a sparse matrix!
How does one do this? How should the c code look like so that it can readily be converted into a mex function?
- Nithin
Answers (1)
Kaustubha Govind
on 19 Mar 2012
0 votes
You'll probably need to write this directly as a MEX-function because there's no C equivalent to a cell-array. See Passing Structures and Cell Arrays to find out how to use cell-arrays in MEX-functions.
In particular, the examples phonebook.c and explore.c in $matlabroot/extern/examples/mex are a good place to start learning about using cell-arrays.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!