Overwriting information in a cell array

6 次查看(过去 30 天)
I am working with cell arrays and matrices. I am trying to overwite information in a cell but instead it places the new data into a new cell and retains a copy of the old data in a deeper cell if that makes sense. As a result, when I try to index into the cells I am grabbing the old data instead of the information I am looking for. I read the cell array page and thought I understood, but I guess I am missing something. The cell array in question is BranchMatrixRevised. If someone could help me either index into the proper cell or overwrite the old information that would be great. My code is below.
clear
close
clc
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;0.834848920291403,3.20098548783449,7.80716223363894;2.23645813228566,-1.80307906039568,7.80775218019269;-4.13663420978661,6.40608142770811,2.87446730769027;-8.01389420090031,0.739303774953617,2.00147044244114;8.04005085003118,2.54735749010561,3.65763161659614;-3.28567882346993,-8.41947317913047,3.80597816799600;0.151573909742903,2.03767830086445,10.7605659520551;-6.82918401328041,0.278600280775844,2.52363667248442;-3.41363523840503,-7.08870431609608,4.14330417016278;6.35396040499232,-0.474663609164808,3.19491420795479;1.06216210967955,4.10256481833496,8.17524296492560;2.75275937858220,-2.40442829800382,8.41751616527277;-4.49887555491066,7.27474744137621,3.21240304800297;-8.93747200633802,0.809847032829654,2.37833604988111;8.93679366464217,2.75035659507643,4.05087923302196;-3.49679202503365,-9.27121011623096,4.28553567873291;0.120474891403918,2.30220475044321,11.7244428158357;-6.57337445354577,-0.109648927503134,3.40897503798041;-3.90131441220521,-6.94494653825290,5.00440969491056;7.05097560070378,-1.18529470189817,3.29069138158143;1.32262284552297,4.97356081042056,8.59180771001900;3.28964279764057,-2.99261263184027,9.02232637002263;-4.85227421135047,8.14355948795292,3.55920993569369;-9.85594939518955,0.878289437178573,2.76784204913582;9.82550611659926,2.98592839011066,4.44419349302377;-3.72248319429006,-10.1171476702764,4.76870557719025;0.0788524583019931,2.54046289454804,12.6947523694476;2.70499218347696,-3.21391766868317,7.83232794441650;-2.84203085926377,-9.70887276953909,3.66930583711619;-6.31756489381112,-0.497898135782113,4.29431340347640;-4.51675110863053,-6.94038338635526,5.79258280428302;7.75826716048403,-1.87839080959415,3.42981878707837;1.60739083428736,5.83327554833172,9.01583337197052;3.81380630124427,-3.57090289370221,9.64749274349896;-5.21110077179350,9.00941019096056,3.90784450027642;-10.7701023523524,0.936847376158581,3.16895966118096;10.7111773842992,3.23085322898537,4.83865290649108;-3.95158917741763,-10.9629681728992,5.25047121091495;0.0248941422755761,2.76290060345524,13.6682049701363;-3.96468559986903,8.59207273742804,3.45422268973630;-4.53038653298116,-10.2195319906366,4.18835254129213;-0.734431529015096,3.08504028776024,12.4898020857790;-2.18726969349390,-10.1465354228472,3.05307599549947;-5.19378789303215,-7.06258274529947,6.51831588830272;8.48339326062922,-2.55215288868641,3.57207489297718;1.87742850700644,6.69346716517060,9.44844178339196;4.34166392226097,-4.13247115672692,10.2846798572246;-5.56586475306079,9.87609452074379,4.25855918503254;-11.6790955983398,1.00013474235130,3.58093780141378;11.5912722790929,3.48104407126623,5.24218421704439];
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
SizeofA = size(A,1);
dist1a = nan(numel(x)); % places nans on the diagonal after distance calculation
proximity = 1.000;
save_criteria = 3;
for i = 1:SizeofA
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);
keep_x1 = x(i2keep);
keep_y1 = y(i2keep);
keep_z1 = z(i2keep);
B = [keep_x1, keep_y1, keep_z1];
C = 1:SizeofA;
index = ismember(C,i2keep);
D = A(~index,:);
%%
dist1b = pdist2(B, D);
[~, minRowIdx] = min(dist1b,[],1);
[GroupID, GroupList1] = findgroups(minRowIdx);
NeighborGroups1 = splitapply(@(x){x},D,GroupID(:));
SizeNG1 = size(NeighborGroups1,1);
%%
for i = 1:SizeNG1
test1{i} = pdist2(B(GroupList1(i),:), NeighborGroups1{i});
end
%%
for i = 1:SizeNG1
test4a = find(test1{1,i} - proximity <= eps('single'));
test4b{i} = test4a;
end
%%
for i = 1:size(B,1)
[BranchMatrix{i}]= BranchLinks(B(i,:));
end
%%
for i = 1:size(B,1)
[NeighborMatrix] = NeighborDetector(test4b{i},NeighborGroups1{i});
BranchMatrix{1,i} = [BranchMatrix(i), NeighborMatrix];
end
%%
x_c = 0;
y_c = 0;
z_c = 0;
Center = [x_c,y_c,z_c];
for i = 1:length(BranchMatrix)
dist1c(i,:) = pdist2(Center,BranchMatrix{1,i}{1,1});
end
%%
BranchMatrixRevised = BranchMatrix;
for i = 1:length(BranchMatrixRevised)
[DownstreamMatrix] = DownstreamDetector(Center,dist1c(i),BranchMatrix{1,i}{1,2});
BranchMatrixRevised{1,i}= [BranchMatrixRevised(i), DownstreamMatrix];
end
%%
for i = 1:length(BranchMatrixRevised)
dist1e{i,:} = pdist2(BranchMatrix{1,i}{1,2},D);
end
%%
function [NeighborMatrix] = NeighborDetector(test4b,NeighborGroups1)
for i = 1:length(test4b)
NeighborMatrix(i,:) = NeighborGroups1(test4b(i),:);
end
end
function [BranchMatrix] = BranchLinks(B)
BranchMatrix = B;
end
function [DownstreamMatrix] = DownstreamDetector(Center,dist1c,BranchArray)
dist1d = pdist2(Center,BranchArray);
for i = 1:size(BranchArray,1)
if dist1c > dist1d(i) && length(dist1d) > 2 && dist1d(i) == min(dist1d)
DownstreamMatrix(i,:) = NaN(1,3);
else
DownstreamMatrix(i,:) = BranchArray(i,:);
end
end
end

采纳的回答

Sindar
Sindar 2020-11-9
编辑:Sindar 2020-11-9
Braces access cell contents, allowing you to update (in this case, appending to the end and hoping it's a compatible shape)
BranchMatrixRevised{1,i}= [BranchMatrixRevised{i}, DownstreamMatrix];
parentheses access the ith cell, not it's contents
  2 个评论
Sindar
Sindar 2020-11-9
In my opinion, your original line should throw an error, since you are trying to concatenate a cell and something that is presumably numeric. But, it turns out not to be the case, as we see here:
x=[{1} 1]
x =
1×2 cell array
{[1]} {[1]}
James Peach
James Peach 2020-11-9
Oh Ok I see what you mean. I was trying to edit the left hand side of the statement but my problem was on the right hand side in terms of overwriting the appropriate cellls. I see now so I stopped appending the information decided just to index directly into the cells and replace them. Thank you Sindar!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by