myrandint.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/REC-html40/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Description of myrandint</title>
  6. <meta name="keywords" content="myrandint">
  7. <meta name="description" content="MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries">
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  9. <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  10. <meta name="robots" content="index, follow">
  11. <link type="text/css" rel="stylesheet" href="../../m2html.css">
  12. <script type="text/javascript">
  13. if (top.frames.length == 0) { top.location = "../../index.html"; };
  14. </script>
  15. </head>
  16. <body>
  17. <a name="_top"></a>
  18. <!-- ../menu.html chronux_2_10 --><!-- menu.html test -->
  19. <h1>myrandint
  20. </h1>
  21. <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  22. <div class="box"><strong>MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries</strong></div>
  23. <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  24. <div class="box"><strong>function ranInt = myrandint(outputRow,outputCol,outputRange,varargin) </strong></div>
  25. <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  26. <div class="fragment"><pre class="comment"> MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries
  27. drawn with replacement from elements of vector RANGE. The elements in
  28. vector RANGE do not need to be contiguous or unique. (Actually, they do
  29. not even need to be integers: The function works the exact same way with
  30. noninteger elements, but a warning is generated to alert the user that
  31. noninteger elements are being sampled.)
  32. To specify a contiguous integer range from Xlow to Xhi, use RANGE = [Xlow:Xhi].
  33. MYRANDINT(M,N,RANGE,'noreplace') is an M-by-N matrix with random integers
  34. drawn without replacement.
  35. This function is based around RAND and RANDPERM, and is intended as a
  36. modest imitation of Comm Toolbox's RANDINT.</pre></div>
  37. <!-- crossreference -->
  38. <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  39. This function calls:
  40. <ul style="list-style-image:url(../../matlabicon.gif)">
  41. </ul>
  42. This function is called by:
  43. <ul style="list-style-image:url(../../matlabicon.gif)">
  44. <li><a href="testAvg3.html" class="code" title="">testAvg3</a> This is a calling routine to test & check out the power spectrum &</li><li><a href="testAvg4.html" class="code" title="">testAvg4</a> This is a calling routine to test & check out the power spectrum &</li></ul>
  45. <!-- crossreference -->
  46. <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  47. <div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)</a>
  48. 0002 <span class="comment">% MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries</span>
  49. 0003 <span class="comment">% drawn with replacement from elements of vector RANGE. The elements in</span>
  50. 0004 <span class="comment">% vector RANGE do not need to be contiguous or unique. (Actually, they do</span>
  51. 0005 <span class="comment">% not even need to be integers: The function works the exact same way with</span>
  52. 0006 <span class="comment">% noninteger elements, but a warning is generated to alert the user that</span>
  53. 0007 <span class="comment">% noninteger elements are being sampled.)</span>
  54. 0008 <span class="comment">%</span>
  55. 0009 <span class="comment">% To specify a contiguous integer range from Xlow to Xhi, use RANGE = [Xlow:Xhi].</span>
  56. 0010 <span class="comment">%</span>
  57. 0011 <span class="comment">% MYRANDINT(M,N,RANGE,'noreplace') is an M-by-N matrix with random integers</span>
  58. 0012 <span class="comment">% drawn without replacement.</span>
  59. 0013 <span class="comment">%</span>
  60. 0014 <span class="comment">% This function is based around RAND and RANDPERM, and is intended as a</span>
  61. 0015 <span class="comment">% modest imitation of Comm Toolbox's RANDINT.</span>
  62. 0016
  63. 0017
  64. 0018 <span class="keyword">if</span> isequal(size(outputRange),[1 2]) &amp;&amp; ~isequal(outputRange(1),outputRange(2)-1),
  65. 0019 warning(<span class="string">'To specify a range [low high] use [low:high].'</span>)
  66. 0020 <span class="keyword">end</span>
  67. 0021 <span class="keyword">if</span> ~isequal(round(outputRange),outputRange),
  68. 0022 warning(<span class="string">'Specified RANGE contains noninteger values.'</span>)
  69. 0023 <span class="keyword">end</span>
  70. 0024 <span class="keyword">if</span> ~isequal(length(outputRange),length(outputRange(:))),
  71. 0025 error(<span class="string">'Range must be a vector of integer values.'</span>)
  72. 0026 <span class="keyword">end</span>
  73. 0027
  74. 0028 numElements = outputRow*outputCol;
  75. 0029
  76. 0030 <span class="keyword">if</span> isempty(varargin),
  77. 0031
  78. 0032 ranInt = zeros(outputRow,outputCol);
  79. 0033 randIx = floor((length(outputRange))*rand(size(ranInt))) + 1;
  80. 0034 ranInt = outputRange(randIx);
  81. 0035 <span class="keyword">if</span> ~isequal(size(randIx),size(ranInt)),
  82. 0036 ranInt = reshape(ranInt,size(randIx));
  83. 0037 <span class="keyword">end</span>
  84. 0038
  85. 0039 <span class="keyword">elseif</span> isequal(varargin{1},<span class="string">'noreplace'</span>),
  86. 0040
  87. 0041 <span class="keyword">if</span> numElements &gt; length(outputRange),
  88. 0042 error(<span class="string">'Not enough elements in range to sample without replacement.'</span>)
  89. 0043 <span class="keyword">else</span>
  90. 0044 <span class="comment">% Generate full range of integers</span>
  91. 0045 XfullShuffle = outputRange(randperm(length(outputRange)));
  92. 0046 <span class="comment">% Select the first bunch:</span>
  93. 0047 ranInt = reshape(XfullShuffle(1:numElements),outputRow,outputCol);
  94. 0048 <span class="keyword">end</span>
  95. 0049
  96. 0050 <span class="keyword">else</span>
  97. 0051 error(<span class="string">'Valid argument is ''noreplace''.'</span>)
  98. 0052 <span class="keyword">end</span>
  99. 0053
  100. 0054</pre></div>
  101. <hr><address>Generated on Fri 12-Aug-2011 11:36:15 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
  102. </body>
  103. </html>