home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 7.ddi / ROBUST.DI$ / GRAFT.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  3.2 KB  |  136 lines

  1. function tr1=graft(tr,b,nm)
  2. % TR1 = GRAFT(TR,B) 
  3. % TR1 = GRAFT(TR,B,NM) adds root branch B onto tree vector TR (previously
  4. %     created by TREE or MKSYS). If TR has N branches then the numerical 
  5. %     index of the new root branch is N+1; and the numerical indices
  6. %     of the other root branches are unchanged.  The string NM, if 
  7. %     present, becomes the name of the new root branch.  Related functions
  8. %     include the following:
  9. %         TREE     creates a tree
  10. %         MKSYS    creates a tree containing matrices describing a system
  11. %         BRANCH   returns branches of a tree
  12. %         ISTREE   tests whether a variable is a tree or not
  13. %         ISBRANCH testw whether a give branch of a tree exists
  14. %
  15.  
  16. % R. Y. Chiang & M. G. Safonov 07/01/91
  17. % Copyright (c) 1991 by the MathWorks, Inc.
  18. % All Rights Reserved.
  19. % ---------------------------------------------------------------------------
  20.  
  21. if nargin<2, error('must specify at least two input arguments'),end
  22. if nargin<3, nm='';end
  23.  
  24. % Remove any spaces from NM
  25. nm=[nm ' ']; nm(find(nm==' '))='';
  26. % Now check name NM for validity
  27. temp=find((nm>='0' & nm<='9')|(nm>='A' & nm<='Z')|nm=='_'|(nm>='a' & nm<='z'));
  28. if min(size(nm))>0
  29.    temp=nm(temp);
  30.    if ~issame(temp,nm) | ~(nm(1)>=65),
  31.      msg=..
  32.      ['Third argument must be a string containing a valid Matlab variable name.'];
  33.      error(msg),
  34.    end
  35. end
  36.  
  37.  
  38. if min(size(tr))==0,
  39.    tr1=tree(nm,b);
  40.    return,
  41. end
  42.  
  43. if ~istree(tr)
  44.  error('The second input argument must be a tree vector created by TREE or MKSYS'),
  45. end
  46.  
  47. % Get HEADER, PTR, NM and DAT vectors from TR and determine pointer IND
  48. c=max(size(tr));
  49. header=tr(1:5);
  50. n=header(1);  % number of branches in TR
  51. ptr=tr(6:6+n);
  52. dat=tr(7+n:c);
  53.  
  54. nm0=branch(tr,0);  
  55. % Determine length NM0 of original branch B0
  56. if ptr(1)>6,
  57.   [rnm0,cnm0]=size(nm0);
  58.   lb0=2+cnm0;
  59. else
  60.   lb0=0;
  61. end
  62.  
  63. % Get DAT1=[B1;...,BN]
  64. dat1ptr0=n+7+lb0;
  65. dat1=tr(dat1ptr0:c);
  66.     
  67.  
  68. % Augment branch B0 with new branch name NM
  69. nm0=branch(tr,0);   % old NM
  70. [rnm0,cnm0]=size(nm0);
  71. [rnm,cnm]=size(nm);
  72. if cnm0>0,
  73.   nm=[nm0 ','  nm];
  74. else
  75.   if cnm > 0,
  76.     nm=[ ( ','*ones(1,n) )  nm];
  77.   end
  78. end
  79. [rnm,cnm]=size(nm);
  80. if cnm>0,
  81.   b0=[-rnm;cnm; nm(:)];
  82. else
  83.   b0=[];
  84. end
  85. [lb0new,junk]=size(b0);
  86. delta=lb0new-lb0+1;
  87.  
  88.  
  89.  
  90. % Now build branch B_{N+1}
  91. % and add unadjusted pointer entry
  92. [rb,cb]=size(b);
  93. if max([rb,cb])==0,
  94.    bn=[];
  95.    ptr=[ptr;1];
  96. else
  97.    if isstr(b), rb=-rb;end         % Negative rb indicates string
  98.    bn=[rb;cb;b(:)];
  99.    ptr=[ptr;c+1];
  100. end
  101.  
  102. % Update DAT vector
  103. dat = [b0;dat1;bn];
  104.  
  105.  
  106. % Update N  and HEADER
  107. n=n+1;
  108. header(1)=n;
  109.  
  110. % Update PTR(1) (which points to branch B0)
  111. if lb0new>0,
  112.   ptr(1)=n+7;  % points to branch B0 data
  113. else
  114.   ptr(1)=1;  % pointer for empty matrix is 1
  115. end
  116.  
  117. % Update PTR(2:N+1)
  118. temp=ptr(2:n+1);
  119. i=find(temp>5);
  120. nptr=max(size(i));
  121. if min(size(i))>0,
  122.    temp(i)=temp(i)+delta*ones(nptr,1);
  123. end
  124. ptr(2:n+1)=temp;
  125.  
  126.  
  127.  
  128. % Now assemble the tree vector TR
  129. % Format:
  130. % index     1        6       PTR(0)  ...      PTR(N)
  131.       %     HEADER   PTR     B0dat   ...      BNdat ];
  132. tr1 = 1*[header; ptr; dat]; 
  133. % Note:  Multiplying TR by 1 unsets string bit, if needed
  134.  
  135. % ------ End of GRAFT.M --- RYC/MGS 10/05/90 %
  136.