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 次查看(过去 30 天)
[~,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 个评论
Bruno Luong
Bruno Luong 2022-10-12
" 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.
dpb
dpb 2022-10-12
编辑:dpb 2022-10-12
I guess I misinterpreted the plural in "I have about 200 cell arrays..." as 200 variables, not a single cell array of size 200.

请先登录,再进行评论。

回答(1 个)

Bruno Luong
Bruno Luong 2022-10-12
  3 个评论
Bruno Luong
Bruno Luong 2022-10-15
编辑:Bruno Luong 2022-10-15
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{:}
ans = 1×10
54 4 1 75 9 2 5 41 68 3
ans = 1×7
3 2 49 5 76 1 4
ans = 1×8
91 17 3 2 4 1 5 95
ans = 1×10
38 3 79 92 2 62 66 1 5 4
ans = 1×6
71 5 3 1 2 4
ans = 1×10
76 4 1 3 5 2 40 57 75 48
ans = 1×9
1 3 89 39 90 4 64 5 2
ans = 1×9
36 47 1 3 14 73 4 5 2
ans = 1×9
3 5 45 86 5 4 59 1 2
ans = 1×10
27 1 5 3 5 2 4 47 63 2
mintersect(C{:})
ans = 1×5
1 2 3 4 5
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
Image Analyst 2022-10-15
@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:

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by