Makes a String representation of a matrix. - Android java.lang

Android examples for java.lang:Math Matrix

Description

Makes a String representation of a matrix.

Demo Code

/*//from  ww w . ja  v  a  2  s .  co  m
 * Copyright 2012 Dan Mercer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    /**
     * Makes a String representation of a matrix.
     * @param mat The <code>float</code> array containing the matrix.
     * @param matOffset The offset into <code>mat</code> where the matrix starts.
     * @return The String representation of the matrix, with all numbers printed to 4 digits of precision.
     */
    public static String matrixToString(float[] mat, int matOffset) {
        final String rowFormat = "% .8f, % .8f, % .8f, % .8f\n";
        final StringBuilder sb = new StringBuilder();

        for (int row = 0; row < 4; row++) { // For each row
            sb.append(String.format(rowFormat, mat[matOffset + row],
                    mat[matOffset + row + 4], mat[matOffset + row + 8],
                    mat[matOffset + row + 12]));
        }
        return sb.toString();
    }
}

Related Tutorials