CMATRESAMP Resamples a cycle matrix. CALL: FF = cmatresamp(F) F = Cycle matrix. [n,n] FF = Resampled cycle matrix. [n,n] Resamles by picking the cycles at random with frequencies specified by F. The result FF contains the same number of cyles as in F. Example: F = round(5*triu(rand(4,4),1)) FF = cmatresamp(F)
Random arrays from the binomial distribution. | |
Display message and abort function. | |
Convert sparse matrix to full matrix. | |
Create sparse matrix. | |
1-D Table look-up. |
Quick test of the routines in module 'cycles' |
001 function FF = cmatresamp(F,Method) 002 %CMATRESAMP Resamples a cycle matrix. 003 % 004 % CALL: FF = cmatresamp(F) 005 % 006 % F = Cycle matrix. [n,n] 007 % FF = Resampled cycle matrix. [n,n] 008 % 009 % Resamles by picking the cycles at random with frequencies specified by F. 010 % The result FF contains the same number of cyles as in F. 011 % 012 % Example: 013 % F = round(5*triu(rand(4,4),1)) 014 % FF = cmatresamp(F) 015 016 % See also cmatplot 017 018 % TODO % Remove dependence on stats toolbox, i.e., remove call to binornd 019 % Check input arguments 020 ni = nargin; 021 no = nargout; 022 error(nargchk(1,2,ni)); 023 024 if ni<2, Method=[]; end 025 026 % Default values, vectorized calculations 027 if isempty(Method), Method=3; end % Vectorized calculations 028 029 030 [m,n]=size(F); 031 N=sum(sum(F)); 032 R=rand(N,1); 033 034 switch Method 035 % Method 1: Binomial sampling 036 case 1, 037 F1=F(:); 038 I=F1>0; 039 F2=F1(I); 040 FF2 = zeros(size(F2)); 041 NN=N; 042 for i=1:length(F2) 043 FF2(i) = binornd(NN,F2(i)/NN); 044 NN=NN-FF2(i); 045 end 046 FF1=zeros(size(F1)); 047 FF1(I)=FF2; 048 FF=reshape(FF1,m,n); 049 050 % Method 2: sum of Multinomial I 051 case 2 052 053 F1=F(:); 054 I=find(F1>0); 055 F2=F1(I); 056 tab = [[0;cumsum(F2)/sum(F2)] (0:length(F2))']; 057 R=rand(N,1); 058 x=ceil(table1(tab,R)); 059 FF1=full(sparse(I(x),1,1,m*n,1)); 060 FF=reshape(FF1,size(F)); 061 062 % Method 3: sum of Multinomial II 063 case 3 064 065 F1=F(:); 066 I=find(F1>0); 067 F2=F1(I); 068 Fn = cumsum(F2)/sum(F2); 069 FF2 = zeros(size(F2)); 070 for k = 1:N 071 x=sum(rand>Fn)+1; 072 FF2(x)=FF2(x)+1; 073 end 074 FF1=zeros(size(F1)); 075 FF1(I)=FF2; 076 FF=reshape(FF1,m,n); 077 078 end 079 080 081 082
Comments or corrections to the WAFO group