function [ v ] = markovchain( A, init, steps ) %MARKOVCHAIN Summary of this function goes here % Detailed explanation goes here % Check that A is a matrix if ndims(A) ~= 2 throw(MException('markovchain:A','A is not a matrix')); end % Check that A is a square matrix [rowsA colsA] = size(A); if rowsA ~= colsA throw(MException('markovchain:A','A is not a square matrix')); end % Check that A is a transition matrix (i.e. non-negative entries, rows % sum to 1). if min(min(A)) < 0 throw(MException('markovchain:A','A is not a matrix with non-negative entries')); end rowsumsA = sum(A,2); if min(rowsumsA) < 1 || max(rowsumsA) > 1 throw(MException('markovchain:A','A is not a matrix with rows that sum to 1')); end % Check that init is a compatible vector [ rowsinit colsinit ] = size(init); if rowsinit ~= 1 || colsinit ~= rowsA throw(MException('markovchain:init','init is not the right size')); end % Check that steps is a positive integer if steps <= 0 throw(MException('markovchain:steps','steps must be positive')); end if steps ~= round(steps) throw(MException('markovchain:steps','steps must be an integer')); end v = init*(A^steps); end