Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: MEX and structure of arrays
Replies: 1   Last Post: Feb 12, 2013 11:50 AM

 Messages: [ Previous | Next ]
 Vincent Posts: 5 Registered: 6/6/12
MEX and structure of arrays
Posted: Feb 12, 2013 11:07 AM

Hi all,

I?m looking for some advice in a project in which speed and performance is of great importance.

I created a model which consists of an outer function with 1 for-loop (25 000 iterations) which invokes several custom-made functions. To increase model?s performance, I rewrote the custom-made functions into MEX-functions. This already gave a good speed-up. However, I would also like to place the outer for-loop in a MEX-function. But I have a problem passing data from MATLAB to the MEX-functions. (Note that it?s not possible to vectorize the for-loop).

The data which is known prior to the model?s simulation is saved to a large nested structure of arrays (built in the form of ?data.Q.position1? with ?position1? an array of 10000 doubles). All arrays containing doubles have the same length. The results of the model will also be written to this structure of arrays (different fieldnames of course, and pre-allocation is done before the for-loop)

So currently, the model is looking like this:

[data] = function model(data)
for i=1:10000
data.Q.position1(i) = MEX-function1(data.WL.position4(i), data.WL.position3(i), ?);
data.Q.position2(i) = MEX-function1(data.WL.position6(i), data.WL.position12(i), ?);
?
data.WL.position7(i) = MEX-function2(data.Q.position1(i), ?);
?
end

I chose for the structure of arrays so I can point easily at the required variables for the function inputs.

So my question is, how can I place the for-loop in a MEX function and what do I have to do with the structure? I?m really new to MEX and C-language, so maybe there?s a very easy answer =)

Secondly, does this whole concept looks good with regard to performance, or are better solutions available?