Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
-
Michael Fanton
- Posts: 11
- Joined: Sun Jan 22, 2017 12:21 pm
Post
by Michael Fanton » Wed Feb 06, 2019 7:36 pm
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!
Tags:
-
Dimitar Stanev
- Posts: 1096
- Joined: Fri Jan 31, 2014 5:14 am
Post
by Dimitar Stanev » Fri Feb 08, 2019 1:27 am
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.
-
jimmy d
- Posts: 1375
- Joined: Thu Oct 04, 2007 11:51 pm
Post
by jimmy d » Fri Feb 08, 2019 11:21 am
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.
-
Michael Fanton
- Posts: 11
- Joined: Sun Jan 22, 2017 12:21 pm
Post
by Michael Fanton » Fri Feb 08, 2019 5:29 pm
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?
-
jimmy d
- Posts: 1375
- Joined: Thu Oct 04, 2007 11:51 pm
Post
by jimmy d » Tue Feb 12, 2019 8:07 am
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.