Page 1 of 1

efficient conversion of opensim matrix to Matlab matrix

Posted: Wed Feb 06, 2019 7:36 pm
by mfanton
Hello,

I am working on a project where I need to convert a large number (ideally 40000+) of Opensim matrices (mass matrices of a model in different configurations) into matlab matrices. I've written a short function which converts Opensim Matrix() into a Matlab matrix using nested for loops, but this is quite inefficient and converting just a few thousand of these matrices can take over 48 hours or longer. See code:

Code: Select all

% convert an osim Matrix() to matlab matrix

function mat = osimMatrixToMatlab(p)
% import java libraries
import org.opensim.modeling.*
% Check the input type
if strcmp(class(p), 'org.opensim.modeling.Matrix')
     % Convert the input Opensim Vec3 to a Matlab Vector
     nrow = p.nrow();
     ncol = p.ncol();
     mat = zeros(nrow,ncol);
     for i=0:nrow-1
         for j=0:ncol-1
             mat(i+1,j+1) = p.get(i,j);
         end
     end
else
    error('Incorrect class input. Must be type org.opensim.modeling.matrix') 
end

end
I was wondering if anyone had any recommendations for how to make this process more efficient. I've done a bit of searching but wasn't able to find any alternatives that don't use nested for loops. I also have a second somewhat related question - is it possible to do simple matrix algebra (multiplying, etc) with Opensim Matrices within the Matlab environment?

Thanks for your help!

Re: efficient conversion of opensim matrix to Matlab matrix

Posted: Fri Feb 08, 2019 1:27 am
by mitkof6
I don't think that this is possible using with the current API, unless you copy the content of the memory. It may be possible to make a view of the SimTK::Matrix or SimTK::Vector as a Matlab array through mex but I am not how difficult it can be.

Re: efficient conversion of opensim matrix to Matlab matrix

Posted: Fri Feb 08, 2019 11:21 am
by jimmy
Opensim matrices (mass matrices of a model in different configurations) into matlab matrices.
DImitar is correct, because these are two completely different types, written in different languages (C++ and Java), it is difficult to cast one to the other. We don't have a more effecient way of doing this at the moment-- even our own methods for converting an OpenSim table to a Matlab Struct goes element by element.

If performance is an issue, then I suggest that you do most of your programming on the C++ so that you can work with the OpenSim matrices directly.

Re: efficient conversion of opensim matrix to Matlab matrix

Posted: Fri Feb 08, 2019 5:29 pm
by mfanton
Sounds good, thanks for your help!

As a follow on question - do you know whether it is easy to manipulate OpenSim matrices (multiply, etc.) within Matlab, or would you recommend also moving to C++ if this is necessary?

Re: efficient conversion of opensim matrix to Matlab matrix

Posted: Tue Feb 12, 2019 8:07 am
by jimmy
It isn't easy. Again, there is a type system built into SimBody (OpenSim's dynamics Library) so there are defined ways to do Matrix multiplication, addition, etc, in the C++ environment. Once you move to Matlab, that breaks down.