Resolving discrepancy between the number of surfaces generated and the number of surface handles stored in cell array

1 Ansicht (letzte 30 Tage)
I am running an algorithm that generates different sized surfaces. However, when I try to store the surface handles in a cell array, I end up with more surface handles stored (59) than surfaces generated (50). I store the surface handles in the cell array h_SurfMatrix. Any help would be greatly appreciated.
clear
close all
clc
%A = 50 pts
A = [0,0,0;0.00977495601952172,0.0129188554738323,0.999868768093125;-0.566794094824837,-0.823750570492204,0.0133959578031223;0.0279587435128966,0.0380588867245362,1.99938731654588;0.830388266617646,0.583999369869120,0.978401571089338;-1.07826433834531,-1.64452537544960,0.267810795303313;0.168715496407312,0.263085998125572,2.96351922435162;-0.791458202545459,0.611268411797120,0.998070417818667;-1.41124221175034,-0.289407593850698,0.0506110237693017;1.69942938355116,1.04462646221878,1.15892918358242;-1.55216375501531,-2.44987966243271,0.623934110066297;0.188310075544404,0.501770043093754,3.93441879647330;-1.44603145623221,1.35107113546187,1.15371676572365;-2.35391403973316,0.0183196401577155,0.179737992978120;2.59879677816763,1.38804628951095,1.42948622326796;-1.92629974765512,-3.28302931738291,1.03122259685150;0.190461468181228,0.731318108759712,4.90771374511875;-2.01837467713375,2.14014570650778,1.37684130228734;-3.30531517848174,0.217844651708771,0.414313445558867;3.50709118523837,1.65551615551852,1.75113998255253;-2.23204460424917,-4.13488361866807,1.45650407075820;0.193343935566412,0.934427321909631,5.88686559182864;-2.51244231909718,2.97180232130260,1.63030617210704;-4.25650570961388,0.357419056329789,0.689550723301627;4.41845060685608,1.87363023778730,2.10021053665969;-2.47842156351806,-4.99324597672323,1.90651791078471;0.204283419234320,1.13357858075284,6.86677329350173;0.617793464714887,0.0559247730198438,6.10612769252045;-2.96417360323821,3.81704174520463,1.91580426879081;-5.20242439373799,0.476408999682868,0.991344090368801;5.32672360772571,2.06445068867013,2.47253796167180;-2.69346338350278,-5.85030685131976,2.37470982965291;0.218591936197924,1.32878562119851,7.84743096960627;-3.26247612265381,-4.91985596911837,1.29018005288354;1.20197539596291,-0.580347098756520,6.61000225666269;-3.37592827171665,4.67443410026770,2.22457026832058;-6.14584567995603,0.584770610127789,1.30473528056482;6.23299868743130,2.22581272192086,2.86321400922248;-2.89467385558292,-6.70726873056154,2.84918921113506;0.209717491582492,1.53184472980862,8.82655723451965;5.28440710514136,1.17097130314088,2.91964410258613;0.576278852478300,2.24513662818586,7.66753960818188;-4.04653068178957,-4.84646596151352,0.673842194982372;1.72465217643194,-1.18852390077965,7.20743841278270;-3.76500316313530,5.53715317053961,2.54758193166533;-7.08499357301506,0.666849489054822,1.63829830698843;7.13773556686645,2.38268963160310,3.25924533904186;-3.08850223839479,-7.56458389242937,3.32609724340970;0.176682672356017,1.77636612069925,9.79563823738556;5.70892694024996,0.283366758099107,3.09836478191717];
SizeA = size(A,1);
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
dist1a = nan(numel(x));
proximity = 1.000;
save_criteria = 3;
save_criteria2 = 2;
for i = 1:SizeA
for j = 1:(i-1)
dist1a(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2 + (z(i)-z(j)).^2);
dist1a(j,i) = dist1a(i,j);
end
end
i2keep = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria);
i2keep2 = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria2);
keep_x1 = x(i2keep);
keep_y1 = y(i2keep);
keep_z1 = z(i2keep);
B = [keep_x1, keep_y1, keep_z1];
C = 1:SizeA;
index = ismember(C,i2keep2);
D = A(~index,:);
E = A(index,:);
%%
col = 1;
[master_index,~] = find(~index');
master_index_size = size(master_index,1);
indx = zeros(master_index_size,1);
for i = 1:master_index_size
while master_index(i,end) ~= 1
col = col+1;
indx = master_index(i,col-1);
F = A(indx,:);
master_index(i,col) = knnsearch(A(1:indx-1,:),F);
if master_index(i,col) == 1
break
end
end
col = 1;
end
master_index = flip(master_index,2);
%%
x_c = 0;
y_c = 0;
z_c = 0;
Center_Root = [x_c, y_c, z_c];
[x_dom,y_dom,z_dom] = sphere(80); % Create Sphere
x_dom = x_dom(41:end,:); % Keep top 41 x points
y_dom = y_dom(41:end,:); % Keep top 41 y points
z_dom = z_dom(41:end,:); % Keep top 41 z points
hemisphere_radius = 80;
figure;
Hemi_sf = surf(hemisphere_radius.*x_dom,hemisphere_radius.*y_dom,hemisphere_radius.*z_dom, 'FaceColor','#4DBEEE','EdgeColor', 'none');
alpha 0.2
hold on
radii_plane = 80;
center_plane = [x_c, y_c]; % center point of circular plane
viscircles(center_plane, radii_plane, 'color', '#77AC30');
scale = 1;
s = 4*scale;
used_index = [];
h_SurfMatrix = [];
for i = 1:size(master_index,1)
branch = master_index(i,:);
branch(branch == 0) = [];
for j = 1:length(branch)
if ismember(branch(1,j), i2keep)
s = s*2^(-1/3);
end
if ismember(branch(1,j),used_index)
h{j,:} = [];
continue
end
[x_loc,y_loc,z_loc, spheresXYZ{i,j}] = createspheres(A(branch(1,j),1),A(branch(1,j),2),A(branch(1,j),3), s);
h{j,:} = surf(x_loc+A(branch(1,j),1), y_loc+A(branch(1,j),2), z_loc+A(branch(1,j),3), 'FaceColor', 'k');
h = h(~any(cellfun('isempty', h), 2), :);
used_index = [used_index;branch(1,j)];
end
h_SurfMatrix = [h_SurfMatrix;h];
s = 4*scale;
end
function [X,Y,Z,spheresXYZ] = createspheres(spherex, spherey, spherez, s)
[x, y, z] = sphere(11);
X = (s*x)+spherex;
Y = (s*y)+spherey;
Z = (s*z)+spherez;
spheresXYZ = [X,Y,Z];
end

Akzeptierte Antwort

Adam Danz
Adam Danz am 24 Dez. 2020
Bearbeitet: Adam Danz am 24 Dez. 2020
You're experiencing problems that often occur when loop variables are not preallocated.
  1. When i==1, the j-loop has 10 iterations so h will be a 10x1 cell array.
  2. When i==2, the j-loop has 9 iterations but since you are not preallocating the h-array, h still is 10x1 even though the j-loop only has 9 iterations. So h{10} shouldn't be there when i==2.
To fix that, preallocate h
for i = 1:size(master_index,1)
branch = master_index(i,:);
branch(branch == 0) = [];
h = cell(numel(branch),1); % <--------
for j = 1:numel(branch)
if ismember(branch(1,j), i2keep)
Also, avoid using length(); use size() or numel() instead. Here's a nice discussion on that topic.
So, the main lesson is: always preallocate loop variables.
  1 Kommentar
Vance Blake
Vance Blake am 25 Dez. 2020
Sorry Im just seeing this. The email this account is attached to doesnt sync on my phone no matter what I do. Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by