function [Y] = PickPeriodicValues( X, m, n, p, q ) % Return the sub-matrix composed of all chosen cells % Periodic specification: m, n specify the rows; p, q specify the columns. % If m or n contains 0, then all rows are specified. % If p or q contains 0, then all cols are specified. % The intersection of the row spec & col spec, is chosen. % Example: m=3, n=4 => The 3rd row in each group of 4 rows is specified. % p=2, q=6 => The 2nd col in each group of 6 cols is specified. % The returned matrix contains: % (3, 2), (3, 8), ... % (7, 2), (7, 8), ... etc. % % 15 Jan 2009 - D.Bozarth % 16 Jan Allow m, p to be vectors. % if ~all(m) || ~all(n); m = 0; n = 0; end if ~all(p) || ~all(q); p = 0; q = 0; end Y = []; [h, w] = size(X); for j = 1:h % Justify the modulus operation being zero-centric, % .. with Matlab's starting index of 1. s = j; r = n; %r = mod(j, n); if mod(j, n) == 0; s = n; r = 0; end; %if j == n; r = 0; end; %if any( m == 0 ) || any( m == mod(j, n) ) if any( m == 0 ) || any( m == mod(s, r) ) % The current row is specified for output. if p == 0 % All columns are specified for output. row = [X(j, :)]; else row = []; for k = 1:w %r = q; if j == n; r = 0; end; %if any( p == mod(k, r) ) s = k; r = q; if mod(k, q) == 0; s = q; r = 0; end; if any( p == mod(s, r) ) % The current column is specified for output. row = [row X(j,k)]; end end end Y = [Y ; row]; end end % % end script