I have several multiple cell arrays with different sizes, and I want to find the intersection of all the arrays at the same time
10 views (last 30 days)
Show older comments
[~,ia,ib] = intersect(C{1}(:,2),C{2}(:,2));
B = [C{1}(ia,2:3),C{2}(ib,3)];
Using the above code, I can find the intersection of 2 cell arrays. However, I have about 200 cell arrays with different sizes, how can I find the intersection for all the arrays
3 Comments
Bruno Luong
on 12 Oct 2022
" why you shouldn't create multiple sequentially-numbered variables"
What numbered variables are you refered to? OP has cell C without number that stores various sets.
Answers (1)
Bruno Luong
on 12 Oct 2022
This is what you want
3 Comments
Bruno Luong
on 15 Oct 2022
Edited: Bruno Luong
on 15 Oct 2022
I cannot guess what exactly you have tried and why exactly you still cannot get the answer.
% Generate some dummy data (10 sets that intersect at least in 1:5)
C = cell(1,10);
for k=1:length(C)
rset = [1:5 ceil(100*rand(1,randi(5)))];
rset = rset(randperm(end));
C{k} = rset;
end
C{:}
mintersect(C{:})
function [S,varargout] = mintersect(varargin)
% [S, iA, iB, iC, ...] = mintersect(A, B, C, ...)
% Returns the data S common to numerical vectors A, B, C..., with no
% repetitions. Output S is in sorted order.
% Return in iA, iB, ... index vectors such that S = A(iA) = B(iB) = ...
%
% Syntax: [...] = mintersect(A, B, C, ..., 'rows')
% A, B, are arrays and must have the same number of column n.
% MINTERSECT considers each row of input arguments as an entities
% for comparison. S is array of n-columns, each row appears at least once
% in each input array.
%
% See also: intersect, munion
% Author: Bruno Luong <brunoluong@yahoo.com>
s = varargin(:);
rowflag = ischar(s{end}) && strcmpi(s(end),'rows');
if rowflag
s(end) = [];
end
nsets = size(s,1);
m = cellfun('size',s,1);
n = cellfun('size',s,2);
isallrowv = all(m==1);
if isallrowv
m = n;
n = 1;
else
if any(diff(n))
error('mintersect: input arrays must have the same number of columns');
end
n = n(1);
end
cm = [0; cumsum(m)];
A = zeros(cm(end),n+1);
for k = 1:nsets
r = cm(k)+1:cm(k+1);
A(r,1:n) = s{k};
A(r,end) = k;
end
[v,K] = uniquerow(A);
[u,I,J] = uniquerow(v(:,1:end-1));
tf = accumarray(J,1)==nsets;
S = u(tf,:);
if isallrowv
S = S.';
end
if nargout > 1
nout = nargout-1;
if isempty(S)
out = cell(1,nout);
[out{:}] = deal([]);
else
i = cumsum(accumarray(1+cumsum(m),-m)+1);
iK = i(K);
a = bsxfun(@plus, I(tf), (0:nsets-1));
out = num2cell(reshape(iK(a),size(a)),1);
end
varargout = out;
end
end % mintersect
%%
function [u,I,J] = uniquerow(a)
% perform [u,I,J] = unique(a,'rows') but without overhead
if size(a,2) == 1
[b,K] = sort(a,'ascend');
else
%[b,K] = sortrows(a,'ascend');
[b,K] = sortrows(a); % R2014b does not recorgnize 'ascend' option
end
tf = [true; any(diff(b,1,1),2)];
u = b(tf,:);
%if nargout >= 2
I = K(tf);
if nargout >= 3
J = cumsum(tf);
J(K) = J;
end
%end
end % uniquerow
Image Analyst
on 15 Oct 2022
@Rukundo Wellen, Show the hidden comments to see @Bruno Luong's answer where he had to make up some data because you keep forgetting to attach your own data. Is it surprising that the code didn't work for you when you didn't give us your data? Attach your actual cell arrays in a .mat file. Make it easy for people to help you not hard!
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!