fork download
  1. function c = unique_stable(a)
  2. [c, ~, ic] = unique(a);
  3. ia = arrayfun(@(n) find(ic == n)(1), 1:numel(c));
  4. c = a(sort(ia));
  5. end
  6. function c = unionize_if_intersect(c)
  7. if numel(c) < 2, return, end
  8. for ij = nchoosek(1:numel(c), 2)'
  9. if ~isempty(intersect(c{ij(1)}, c{ij(2)}))
  10. c{ij(1)} = union(c{ij(1)}, c{ij(2)});
  11. c(ij(2)) = [];
  12. c = unionize_if_intersect(c);
  13. break
  14. end
  15. end
  16. end
  17. function c = f(a)
  18. u = unique_stable(cell2mat(cellfun(@(s) {strsplit(s, "=")}, a)));
  19. h = @(k) find(strcmp(u, k))(1);
  20. c = cellfun(@(s) {arrayfun(@(s) h(s), strsplit(s, "="))}, a');
  21. c = cellfun(@(iu) {u(iu)}, unionize_if_intersect(c));
  22. end
  23. a = {"a1=a2", "b1=b2", "b3=b2", "c1=c2", "e1=e2", ...
  24. "a3=a4", "c3=c4", "e1=e3", "a2=a4", "c3=c1", ...
  25. "b3=a4", "c2=d1", "a4=a5", "d2=c1", "b4=b3", "d3=c3"};
  26. disp(['[' strjoin(cellfun(@(s) {['["' strjoin(s, '", "') '"]']}, f(a)), ', ') ']'])
Success #stdin #stdout 0.18s 47600KB
stdin
Standard input is empty
stdout
[["a1", "a2", "b1", "b2", "b3", "a3", "a4", "a5", "b4"], ["c1", "c2", "c3", "c4", "d1", "d2", "d3"], ["e1", "e2", "e3"]]