Preallocation: speeding up loop with variable dimensions
Show older comments
Dear community,
I use this segment of code frequently to make 3D models using the electrical impedance tomography (EIT) package EIDORS. I am producing a multi-layered head model by subtracting the overlapping tetrehedra of STL files of skull, brain, etc. from meshed head models using the MATLAB functions find() and any().
How can I speed up this function given that the (number of) overlapping elements for each layer depend(s) on the geometries of each individual model?
load(meshedHeads); % contains struct with forward model 'fwd_mdlHeadWithElec'
%% first, remove the smallest model from the head (the ventricles)
fwd_mdlvent = stl_read(STLfilenames{v}); % v = first ventricle STL filename
elems_with_nodes_inside_vent_border = []; % ventricle element array we are building
% finds 3D points in solid head model that overlap with ventricles model
node_row_value = intriangulation(fwd_mdlvent.nodes,fwd_mdlvent.elems,fwd_mdlHeadWithElec.nodes);
% isolate points which do indeed overlap
node_row_value_if_inside_border = find(node_row_value); % Gather the ones
% SLOW LOOP, no way to preallocate
for k = 1:size(node_row_value_if_inside_border,1)
%could speed up by indexing by column instead of row? length of this array varies widely based on models:
elems_with_this_node_value = find(any(fwd_mdlHeadWithElec.elems==node_row_value_if_inside_border(k),2));
%concatenate array
elems_with_nodes_inside_vent_border = [elems_with_nodes_inside_vent_border; elems_with_this_node_value];
end
elems_with_nodes_inside_vent_border = unique(elems_with_nodes_inside_vent_border); % remove duplicates
%...repeat for brain, CSF, etc.
%remove elements which represent ventricles from brain model, result is a
% 3D brain model with a cavity in the shape of the ventricles
elems_with_nodes_inside_brain_border = setdiff(elems_with_nodes_inside_brain_border,elems_with_nodes_inside_vent_border);
%...repeat for other model layers, assign conductivities to each layer
%save the model set as a .mat file
save(filename,'headImageCases');
The result is a nested struct with a forward model of the layered-head with shell elements (points) separated by material index.
I hope this explains my problem, though I know it is highly specific. Thank you in advance and I look forward to discussing further with you.
-Jared
**PS - I recognize that it may be helpful for me to provide the full code for this process in the case that my explanations are unclear, but the models and required files make this quite complicated so I hoped that someone may be able to help with just the provided snippet. If including everything will help you to address my problem then please leave a comment and I will take the time to upload/explain the required files and full code.
Accepted Answer
More Answers (0)
Categories
Find more on Neural Simulation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!