How to determine if a row appears more than once in an array in MATLAB

In summary, my program is trying to connect line segments in a cell array by finding the intersection points. However, it only works if the first row in the cell array is a line segment that has only one point in common with another row.
  • #1
roldy
237
2
I'm working on a project for myself that I started some time ago. This question only covers a small part of what I am doing. In my program I have vertices that I read in from a text file. They are read and stored in a matrix where each row of the matrix is a facet (triangle) created by the 2 (x,y) vertices. I end up with a N X 3 matrix of vertices where N is the number of facets. I then pass these through a function that determines what the intersection points are for each facet that is intersected by a plane at x = some value. The intersection points are stored in a N X 2 cell array (say B) where each row is a line segment created by the intersection points of each facet. I wrote a function that takes this cell array and "connects" the line segments by finding the common point (cell) of a row to another row. I have this working to a certain extent. The catch is that it only works if the first row in B is a line segment that has only one point in common with one other row. What I want to do is figure out a way to find which row in B has only one point in common with the one other row. There should be 2 rows in B for this (one for the start and one for the end of the line). Below is the code that I have written thus far. A represents the starting line segment to begin the sorting. Right now A is set as the first row in B in order for the program to run. Try changing A to any other row besides an end line segment to understand what I am getting at.

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
 
Physics news on Phys.org
  • #2
I think the 'isequal' operator might be of some help?

>> A = [1 1 1; 1 1 1;0 0 0;0 0 0]

A =

1 1 1
1 1 1
0 0 0
0 0 0

>> B = [1 1 1]

B =

1 1 1

>> for i=1:size(A,2)
C(i) = isequal(A(i,:),B);
end
>> C

C =

1 1 0 0
 
  • #3
Thanks for the reply. I actually figure this out last night using a combination of ismember and if/then logic statements. This works, however it is in a separate while loop than the one that does the "connecting". Right now I'm trying to optimize the code and trying to figure out how to combine the two while loops.
 

Related to How to determine if a row appears more than once in an array in MATLAB

1. How do I check if a row appears more than once in an array in MATLAB?

To determine if a row appears more than once in an array in MATLAB, you can use the unique function to find the unique rows in the array, and then compare the size of the original array to the unique array. If the sizes are different, it means there are duplicate rows.

2. Can I use a built-in function to determine if a row appears more than once in an array in MATLAB?

Yes, as mentioned before, the unique function can be used to find unique rows in an array. Additionally, the ismember function can be used to check if a specific row appears more than once in the array.

3. What if my array contains NaN values? Will the methods still work?

Yes, the unique and ismember functions can handle arrays with NaN values. However, if you want to ignore the NaN values, you can use the unique(..., 'rows', 'stable') syntax to keep the rows in the same order as the original array.

4. Is there a more efficient way to determine if a row appears more than once in an array in MATLAB?

Yes, if you have a large array and want to improve performance, you can use the sort function to sort the rows of the array and then use the diff function to find the differences between adjacent rows. Rows with a difference of 0 indicate duplicate rows.

5. Can I use logical indexing to determine if a row appears more than once in an array in MATLAB?

Yes, you can use logical indexing to check for duplicate rows in an array. First, create a logical array by comparing each row to the original array. Then, use the any function to check if there are any occurrences of duplicate rows in the logical array.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
13K
  • Precalculus Mathematics Homework Help
Replies
18
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top