Also comparison is quite large. no of rows are 1508 in 'array' and nearly 900 in 'x'

# How to compare rows of an array with other rows?

19 views (last 30 days)

Show older comments

Hey.. How to compare rows of an array with rows defined in x?

array={[1,2,3,4,6,13,18,9];[13];[12,1,2,5];[3,4];[1,5,6]}

x=[1;3]

comparison will be based on x. e.g. first element in x is 1 so it will compare 1st row of array with all other rows. 2nd element is 3 so 3rd row will be compared will all other rows. Comparison will get intersection of elements like:

result{1,1}= {[13;[1,2];[3,4];[1,6]]}

result{2,1}= {[[1,2];0;0;[1,6];[1,5]]}

0 means no common element was found.

please help.

##### 4 Comments

### Accepted Answer

Jan
on 19 Jan 2018

Edited: Jan
on 19 Jan 2018

The question is not clear, but I guess a code which produces the output:

array = {[1,2,3,4,6,13,18,9];[13];[12,1,2,5];[3,4];[1,5,6]}

x = [1;3];

Result = cell(1, numel(x));

n = numel(array);

for ix = 1:numel(x)

index = x(ix);

A = array{index};

B = cell(1, n - 1);

iB = 0;

for ia = 1:n

if ia ~= index

iB = iB + 1;

Inter = intersect(A, array{ia});

if isempty(Inter)

Inter = 0;

end

B{iB} = Inter;

end

end

Result{ix} = B;

end

It might help to save time, if you sort the arrays at first:

for k = 1:numel(array)

array{k} = sort(array{k});

end

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!