- #1

roldy

- 237

- 2

Code:

```
function [line] = connect(B)
clear all
clc
% Build the example cell arrays
A = {[4,-1],[3,0]}; %Initial test row value
B = {[4,-1],[3,0]; %end line segment
[-1,4],[-3,5]; %end line segment
[3,0],[2,1];
[2,1],[-1,4]};
ANumVec = cell2mat(A);
ANum = reshape(ANumVec, 2, 2)';
Bmat = cell2mat(B);
line = A;
m = length(Bmat);
count = 2;
while m ~= 1
I1 = ismember(Bmat(:, 1:2), ANum, 'rows'); %check first two columns
I2 = ismember(Bmat(:, 3:4), ANum, 'rows'); %check last two columns
I = [I1, I2];
B(all(I == 1,2),:) = {[], []}; %replace start row with empty cells to remove from list for next iteration
I(all(I == 1,2),:) = 0; %Find indice of the row in B that contains a matching vertice of the vertices in A
A = Bmat(I > 0,:); %Get next test row vertices
ANum = reshape(A,2,2)';
line(1,count:count+1) = {A(1,[1 2]), A(1,[3 4])}; %Assemble connectivity array
empty_row = ~cellfun('isempty',B);
B = B(empty_row(:,1),:); %Resize B array by removing empty rows
Bmat = cell2mat(B);
[m n] = size(Bmat);
count = count+1;
end
```