# HG changeset patch # User Markus Mützel # Date 1696760026 -7200 # Node ID a476cdefdafc98959cc15c18831b0ba5c223fcee # Parent d079a658d9d9bb59852921204b8f60b897328435# Parent 48599bdcc9ca6dd504c3730d531688afa5a9fa49 maint: Merge stable to default. diff -r d079a658d9d9 -r a476cdefdafc scripts/set/intersect.m --- a/scripts/set/intersect.m Sat Oct 07 20:21:46 2023 -0400 +++ b/scripts/set/intersect.m Sun Oct 08 12:13:46 2023 +0200 @@ -111,7 +111,8 @@ else c = [a; b]; ## FIXME: Is there a way to avoid a call to sort? - c = c(sort (ic(match)), :); + [c_ind, sort_ind] = sort (ic(match)); + c = c(c_ind, :); endif len_a = rows (a); else @@ -132,7 +133,8 @@ else c = [a(:); b(:)]; ## FIXME: Is there a way to avoid a call to sort? - c = c(sort (ic(match))); + [c_ind, sort_ind] = sort (ic(match)); + c = c(c_ind); endif ## Adjust output orientation for Matlab compatibility @@ -147,8 +149,9 @@ if (! optsorted) ## FIXME: Is there a way to avoid a call to sort? ia = sort (ia); - [~, idx] = min (ib); - ib = [ib(idx:end); ib(1:idx-1)]; + ib_ind(sort_ind) = 1:numel(sort_ind); + ## Change ordering to conform to unsorted c + ib(ib_ind) = ib; endif if (optlegacy && isrowvec && ! by_rows) ia = ia.'; @@ -212,6 +215,15 @@ %! c = intersect (a, b, "stable"); %! assert (c, [2,1]); +## Test "stable" argument +%!test <*60347> +%! a = [8 4 2 6]'; +%! b = [1 7 2 8]'; +%! [c, ia, ib] = intersect (a, b, "stable"); +%! assert (c, [8;2]); +%! assert (ia, [1;3]); +%! assert (ib, [4;3]); + %!test %! a = [3 2 4 5 7 6 5 1 0 13 13]; %! b = [3 5 12 1 1 7];