convex

PURPOSE ^

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 This function takes in 2 arrays of data and calculates the convex hull. Then it returns the point whose slope is closest to the input target slope. 
0002 
0003 
0004 function h = convex ( x, y, target , VERBOSE)
0005 % function h = convex ( x, y, target, VERBOSE )
0006 %    find all the points on the convex hull of 2d distribution and
0007 %    return the point whose associated slope is closest to the target
0008 %    slope
0009 %
0010 %    Derived from c function convex.
0011 %
0012 %    this function needs:
0013 %        two arrays of data ( x and y)
0014 %        a target slope
0015 %        VERBOSE =  if included and non zero, this routine will display
0016 %                   each point on the convex hull and it's associated slope
0017 %
0018 %    this function returns:
0019 %        the index of the xy point whose associated slope is the closest match
0020 %        to the target.
0021 %
0022 %    Jon Rogers
0023 
0024 %    $Author: morlighe $
0025 %    $Date: 2009/03/24 21:05:40 $
0026 %    $Id: convex.m,v 1.1 2009/03/24 21:05:40 morlighe Exp $
0027 %    $Source: /home/larour/Ice_Repository/ice1/src/m/utils/Hulls/convex.m,v $
0028 %    $Log: convex.m,v $
0029 %    Revision 1.1  2009/03/24 21:05:40  morlighe
0030 %    reorg
0031 %
0032 %    Revision 1.1  2009/03/19 20:31:59  larour
0033 %    reorg
0034 %
0035 %    Revision 1.1  2009/03/18 14:23:16  larour
0036 %    From root directory
0037 %
0038 %    Revision 1.1  2008/05/21 17:38:54  larour
0039 %    Moved Matlab_Utils to Utils.
0040 %
0041 %    Revision 1.1  2007/11/18 23:19:13  larour
0042 %    New organised MatlabUtils directory.
0043 %
0044 %    Revision 1.5  1996/09/19 23:27:30  jkrogers
0045 %    found a major problem.  if the slope was outside the boundaries, the
0046 %    decision returned was no good.  fixed that.
0047 %
0048 %    Revision 1.1  1996/08/02 07:32:16  jkrogers
0049 %    Initial revision
0050 %
0051 
0052 if (nargin == 3) VERBOSE = 0;
0053 
0054 val = -1;
0055 [mm,nn] = size (x);
0056 if ((mm~=1) & (nn~=1))
0057     fprintf(2,'\nArrays must be 1 dimensional (vectors)');
0058     end
0059 end
0060 [mm,nn] = size (y);
0061 if ((mm~=1) & (nn~=1))
0062     fprintf(2,'\nArrays must be 1 dimensional (vectors)');
0063     end
0064 end
0065 clear mm,nn;
0066 
0067 len = length(x);
0068 
0069 if (len ~= length(y)) 
0070     fprintf(2,'\nArrays must be the same length');
0071     end
0072 end
0073 
0074 elim = zeros (1,len);
0075 slopelist = zeros (1,len);
0076 min_x = 0;
0077 slope = zeros(1,len);
0078 
0079 % find minimum y value of the 2d distribution
0080 cur_y = min(y);
0081 min_idx = find(y==min(y));
0082 
0083 cur_x = x(min_idx);
0084 
0085 % find minimum x value of the 2d distribution
0086 min_x = min(x);
0087 elim(min_idx) = 1;
0088 jj = 1;
0089 
0090 % starting from here, (cur_x, cur_y), look backwards to find the min
0091 % slope between this point and all other points behind it (in x)
0092 while (cur_x ~= min_x) 
0093     smallest = inf;
0094     for (ii = 1:len)
0095         if elim(ii) == 0
0096             if (x(ii) < cur_x)
0097                 candidate = -1*(y(ii)-cur_y)/(x(ii)-cur_x);
0098                 if (candidate < smallest)
0099                     smallest = candidate;
0100                     min_idx = ii;
0101                 end
0102             end
0103         else
0104             elem(ii) = 1;
0105         end
0106     end
0107     cur_y = y(min_idx);
0108     cur_x = x(min_idx);
0109     elem(min_idx) = 1;
0110     slope (jj) = smallest;
0111     slopelist (jj) = min_idx;
0112     jj = jj+1;
0113 end
0114 
0115 % now we have all the slopes on the convex hull.
0116 % find the one closest to the target and return it
0117 if ((slope(1) > target) | (slope(jj) < target))
0118     if (slope (1) > target )
0119         val = slopelist (1);
0120     else
0121         val = slopelist (jj);
0122     end
0123 end
0124 
0125 smallest = abs(slope (slopelist(1)) - target);
0126 val = slopelist(1);
0127 for (ii = 1:jj)
0128     diff = abs(slope(ii)-target);
0129     if (VERBOSE) fprintf(2,'\nPoint : %d\t %f',slopelist(ii),slope(ii));
0130     if (diff < smallest)
0131         smallest = diff;
0132         val = slopelist (ii);
0133     end
0134 end
0135 
0136 h = val;
0137 end
0138 
0139 
0140 
0141 --------------------------------------------------------------------------------
0142 If you have questions/comments... 
0143 
0144 matlabboy@school 
0145     
0146 
0147 previous script | jon's home | information coding lab | next script 
0148

Generated on Fri 27-Mar-2009 18:50:01 by m2html © 2003