Combinations of different size columns (arrays)

3 ビュー (過去 30 日間)
Ahsan Khan
Ahsan Khan 2012 年 9 月 14 日
Hi Matlabers,
How can I get the combination of two columns with different sizes? ex: Column one [1;2;3;4;5] Column two [4;5;6;7;8;9;10]
output:
1 4
1 5
1 6
...
5 4
5 6
5 7
5 8
5 9
5 10
Notice the repeating num don't count (5 and 5). thanx in advance

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 14 日
編集済み: Andrei Bobrov 2012 年 9 月 17 日
a = (1:5)';
b = (4:10)';
[i2,i1] = ndgrid(b,a);
out0 = [i1(:),i2(:)];
out = out0(abs(diff(out0,1,2)) > eps(100),:);
or
a = (1:5)';
b = (4:10)';
idx = fullfact([numel(b),numel(a)])
out0 = [a(idx(:,2)),b(idx(:,1))]
out = out0(abs(diff(out0,1,2)) > eps(100),:);
EDIT
a = (1:5)';
b = (4:10)';
c = [15 17 18]';
d = [20 21 22]';
data = {a,b,c,d};
k = cellfun(@numel,data);
idx = fliplr(fullfact(fliplr(k)));
t = cumsum(k);
idx2 = bsxfun(@plus,idx, [0 t(1:end-1)]);
d2 = cat(1,data{:});
out0 = d2(idx2);
out = out0(any(abs(diff(out0,1,2)) > eps(100),2),:);
  2 件のコメント
Ahsan Khan
Ahsan Khan 2012 年 9 月 14 日
how will this work with more than two arrays. when I add two more arrays (c and d) the output is:
c = [15 17 18];
d = [20 21 22];
[i4,i3,i2,i1,] = ndgrid(d,c,b,a);
out0 = [i1(:),i2(:),i3(:),i4(:)];
out = out0(abs(diff(out0,1,4)) > eps(100),:)
I get: out = Empty matrix: 0-by-4 or maybe I didn't understand the diff function
Andrei Bobrov
Andrei Bobrov 2012 年 9 月 17 日
see EDIT

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 14 日
x=[4;5;6;7;8;9;10]
n=length(x);
y=repmat(1:9,n);y1=y(:)
x1=repmat(x,n*9,1)
v=[y1 x1]
v(x1-y1==0,:)=[]

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by