ELEM_SELECT: select element fractions inside a function memb_frac = elem_select( fmdl, select_fcn ) memb_frac = fraction of each element within the fcn fmdl = fwd_model structure select_fcn = function to describe membership, can also be a cell array of functions, but all must accept parameters x, y, and z. ELEM_SELECT then finds elements that satisfy ALL the functions at once finally, select_fcn can be a string which will then be turned into a function via inline(str,'x','y','z') OR select_fcn = string accepting named variables x,y,z. parameters fwd_model.elem_select.interp_no - interpolation density Example: img = mk_image(mk_common_model('b2d1c',8)); select_fcn = inline('(x-0.2).^2+(y-0.5).^2<0.2^2','x','y','z'); memb_frac = elem_select( img.fwd_model, select_fcn) img.elem_data = 1 + memb_frac*0.1; show_fem(img); Example img = mk_image(mk_common_model('b2d1c',8)); select_fcn = '(x-0.2).^2+(y-0.5).^2<0.2^2'; memb_frac = elem_select( img.fwd_model, select_fcn) img.elem_data = 1 + memb_frac*0.1; show_fem(img); See Also: mk_c2f_circ_mapping
0001 function memb_frac = elem_select( fmdl, select_fcn ) 0002 % ELEM_SELECT: select element fractions inside a function 0003 % memb_frac = elem_select( fmdl, select_fcn ) 0004 % 0005 % memb_frac = fraction of each element within the fcn 0006 % fmdl = fwd_model structure 0007 % select_fcn = function to describe membership, 0008 % can also be a cell array of functions, but all must accept 0009 % parameters x, y, and z. ELEM_SELECT then finds elements that 0010 % satisfy ALL the functions at once 0011 % finally, select_fcn can be a string which will then 0012 % be turned into a function via inline(str,'x','y','z') 0013 % OR 0014 % select_fcn = string accepting named variables x,y,z. 0015 % 0016 % parameters 0017 % fwd_model.elem_select.interp_no - interpolation density 0018 % 0019 % Example: 0020 % img = mk_image(mk_common_model('b2d1c',8)); 0021 % select_fcn = inline('(x-0.2).^2+(y-0.5).^2<0.2^2','x','y','z'); 0022 % memb_frac = elem_select( img.fwd_model, select_fcn) 0023 % img.elem_data = 1 + memb_frac*0.1; 0024 % show_fem(img); 0025 % 0026 % Example 0027 % img = mk_image(mk_common_model('b2d1c',8)); 0028 % select_fcn = '(x-0.2).^2+(y-0.5).^2<0.2^2'; 0029 % memb_frac = elem_select( img.fwd_model, select_fcn) 0030 % img.elem_data = 1 + memb_frac*0.1; 0031 % show_fem(img); 0032 % 0033 % See Also: 0034 % mk_c2f_circ_mapping 0035 0036 if ischar(fmdl) && strcmp(fmdl,'UNIT_TEST'); do_unit_test; return; end 0037 0038 0039 % 4 for 2D, 3 for 3D 0040 dims = size(fmdl.nodes,2); 0041 interp_no = 6 - dims; 0042 try 0043 interp_no = fmdl.elem_select.interp_no; 0044 end 0045 0046 pts = interp_mesh( fmdl, interp_no ); 0047 x = squeeze(pts(:,1,:)); 0048 y = squeeze(pts(:,2,:)); 0049 if dims ==2; 0050 z = 0*x; 0051 else 0052 z = squeeze(pts(:,3,:)); 0053 end 0054 if ischar(select_fcn) 0055 % we have a string, create a function 0056 select_fcn = inline(select_fcn, 'x','y','z'); 0057 memb_frac = mean( feval(select_fcn,x,y,z), 2); 0058 elseif ~iscell(select_fcn) 0059 % the normal case 0060 memb_frac = mean( feval(select_fcn,x,y,z), 2); 0061 else 0062 % many functions case 0063 memb_val = ones(size(x)); 0064 for i = 1:numel(select_fcn) 0065 memb_val = memb_val .* feval(select_fcn{i},x,y,z); 0066 end 0067 memb_frac = mean(memb_val,2); 0068 end 0069 0070 0071 function do_unit_test; 0072 imdl = mk_common_model('a2c2',8); 0073 select_fcn = '(x-0.2).^2+(y-0.5).^2<0.2^2'; 0074 memb_frac = elem_select( imdl.fwd_model, select_fcn); 0075 unit_test_cmp('a2c2 (string)',find(memb_frac), [5, 10,18,26,27]'); 0076 0077 imdl = mk_common_model('a2c2',8); 0078 select_fcn = inline('(x-0.2).^2+(y-0.5).^2<0.2^2','x','y','z'); 0079 memb_frac = elem_select( imdl.fwd_model, select_fcn); 0080 unit_test_cmp('a2c2',find(memb_frac), [5, 10,18,26,27]'); 0081 0082 0083 select_fcn2= inline('y<0.4','x','y','z'); 0084 memb_frac = elem_select( imdl.fwd_model, {select_fcn,select_fcn2}); 0085 unit_test_cmp('a2c2 (2fcns)',find(memb_frac), [5, 10,18,27]'); 0086 0087 imdl = mk_common_model('n3r2',[16,2]); 0088 select_fcn = inline('(x-0.2).^2+(y-0.5).^2 + (z-1).^2<0.1^2','x','y','z'); 0089 memb_frac = elem_select( imdl.fwd_model, select_fcn); 0090 unit_test_cmp('n3r2',find(memb_frac), [156 159 162 168 431 434 437 503]');