Producing a Java-formated matrix in MATLAB -
i have code in matlab gives me matrix @ end. need take matrix java , use there, when matrix big, hard rewrite element element in java. wrote code in matlab:
function javamatrix = generatejavaformatedmatrix(matrix, name) i=1:size(matrix,1) j=1:size(matrix,2) javamatrix(i,j) = char([name '[' num2str(i-1) ']' '[' num2str(j-1) ']=' num2str(matrix(i,j)) ';' ' ']) end end end
which expect trick me, , need copy array , paste in java. example, want above code when write following command:
java_matrix = generatejavaformatedmatrix(eye(3),'a')
is following
a[0][0]=1; a[0][1]=0; a[0][2]=0; a[1][0]=0; a[1][1]=1; a[1][2]=0; a[2][0]=0; a[2][1]=0; a[2][2]=1;
however, getting following error:
subscripted assignment dimension mismatch. error in generatejavaformatedmatrix (line 5) javamatrix(i,j) = char([name '[' num2str(i-1) ']' '[' num2str(j-1) ']=' num2str(matrix(i,j)) ';' ' '])
any highly appreciated.
here code trick.
function s = generatejavaformatedmatrix( a, sigdigs, name ) if nargin < 2 sigdigs = 15; end format = [ '%+1.', int2str(sigdigs), 'e' ]; s = sprintf( 'final double [][] %s = {\n', name ); row = 1:size(a,1) s = [ s, ' { ' ]; col = 1:size(a,2) s = [ s, num2str( a(row,col), format ) ]; if ( col ~= size(a,2) ) s = [ s, ', ' ]; end end if row == size(a,1) s = sprintf( [ s, ' } \n' ] ); else s = sprintf( [ s, ' }, \n' ] ); end end s = [ s, '};' ];
so example, running command
>> generatejavaformatedmatrix( eye(3), 3, 'a' ):
will give:
ans = final double [][] = { { +1.000e+00, +0.000e+00, +0.000e+00 }, { +0.000e+00, +1.000e+00, +0.000e+00 }, { +0.000e+00, +0.000e+00, +1.000e+00 } };
Comments
Post a Comment