Erik Mesoy
Core Tester / Intern
For whatever reason, the MATLAB interface I'm having to work with is screwy; the help file is clogged to lookup-only (not searchable), and so I'm essentially programming from specifications, which is difficult. I'd like help converting the following to MATLAB, which I've written in pseudocode because I know what to do, I just can't get MATLAB to help me in any useful way.
Tildes (~) have been used to help indentation.
So, how should this look in MATLAB?
Tildes (~) have been used to help indentation.
Code:
function makeScoreVector(parameter double[][] stocMatrix, returntype double[]) {
~ double flatteningFactor = 0.15;
~ if (stocMatrix[].size != stocMatrix[][].size) { print "Error. Not a square matrix."; return null; } // can MATLAB identify a square matrix more directly than this method of comparing sizes of first and second dimensions?
~ double[][] resultMatrix = new double[(stocMatrix[].size)][(stocMatrix[][].size)];
~ for int i=0 to stocMatrix[].length {
~ ~ double stochasticCounter = 0;
~ ~ for int j=0 to stocMatrix[][].length {
~ ~ ~ if (stocMatrix[j][i] < 0) { print "Error. Matrix is not stochastic."; return null; } //stochastic requires that all elements are non-negative
~ ~ ~ else {
~ ~ ~ ~ stochasticCounter = stochasticCounter + stocMatrix[j][i];
~ ~ ~ ~ resultMatrix[j][i] = stocMatrix[j][i]*(1-flatteningFactor) + flatteningFactor/stocMatrix[].length; // if flatteningFactor is low, the elements stay close to their original value; otherwise they tend towards 1/n where n is the matrix's size
~ ~ ~ }
~ ~ }
~ if (AbsoluteValueOf(stochasticCounter-1) > 0.0001 ) { print "Error. Matrix is not stochastic."; return null; } //stochastic also requires that the columns must sum to 1; I'm supposed to count four decimals
~ }
~ double[] scoreVector = null(resultMatrix - identityMatrix); //the MATLAB instruction specifics I have gleaned refer to the null function and the use of I for an identity matrix of the appropriate size, with 1 on the diagonals and 0 elsewhere.
~ return scoreVector;
}
So, how should this look in MATLAB?