efficient conversion of opensim matrix to Matlab matrix
Posted: 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:
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!
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
Thanks for your help!