亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

生成包含從n個向量中提取的所有元素組合的矩陣。

生成包含從n個向量中提取的所有元素組合的矩陣。

生成包含從n個向量中提取的所有元素組合的矩陣。這個問題經常以一種或另一種形式出現(例如,參見這里或這里)。所以我想我應該以一種一般的形式呈現出來,并給出一個答案,以供將來參考。給定任意數目n可能大小不同的向量,生成一個n-列矩陣,其行描述從這些向量中提取的所有元素的組合(笛卡爾積)。例如,vectors = { [1 2], [3 6 9], [10 20] }應給予combs = [ 1     3    10           1     3    20           1     6    10           1     6    20           1     9    10           1     9    20           2     3    10           2     3    20           2     6    10           2     6    20           2     9    10           2     9    20 ]
查看完整描述

3 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

這個ndgrid函數幾乎給出了答案,但有一個警告:n必須明確定義輸出變量才能調用它。自n是任意的,最好的方法是使用逗號分隔列表(從單元格數組生成的n作為輸出。結果n然后將矩陣連接到所需的n-欄表:

vectors = { [1 2], [3 6 9], [10 20] }; %// input data: cell array of vectors


n = numel(vectors); %// number of vectors

combs = cell(1,n); %// pre-define to generate comma-separated list

[combs{end:-1:1}] = ndgrid(vectors{end:-1:1}); %// the reverse order in these two

%// comma-separated lists is needed to produce the rows of the result matrix in

%// lexicographical order 

combs = cat(n+1, combs{:}); %// concat the n n-dim arrays along dimension n+1

combs = reshape(combs,[],n); %// reshape to obtain desired matrix




查看完整回答
反對 回復 2019-06-05
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

簡單一點.。如果您有神經網絡工具箱,您可以簡單地使用combvec:

vectors = {[1 2], [3 6 9], [10 20]};combs = combvec(vectors{:}).' % Use cells as arguments

它以稍微不同的順序返回矩陣:

combs =

     1     3    10
     2     3    10
     1     6    10
     2     6    10
     1     9    10
     2     9    10
     1     3    20
     2     3    20
     1     6    20
     2     6    20
     1     9    20
     2     9    20

如果您想要有問題的矩陣,可以使用sortrows:

combs = sortrows(combvec(vectors{:}).')% Or equivalently as per @LuisMendo in the comments: % combs = fliplr(combvec(vectors{end:-1:1}).')

這給了

combs =

     1     3    10
     1     3    20
     1     6    10
     1     6    20
     1     9    10
     1     9    20
     2     3    10
     2     3    20
     2     6    10
     2     6    20
     2     9    10
     2     9    20

如果你看看combvec(類型)edit combvec在命令窗口中,您將看到它使用的代碼與@LuisMendo的答案不同。我不能說哪一個整體上更有效率。

如果碰巧有一個矩陣,其行與以前的單元格數組類似,則可以使用:

vectors = [1 2;3 6;10 20];vectors = num2cell(vectors,2);combs = sortrows(combvec(vectors{:}).')


查看完整回答
反對 回復 2019-06-05
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

我已經對兩種建議的解決方案做了一些基準測試?;鶞蕼y試代碼基于timeit功能,并包括在這篇文章的末尾。

我考慮兩種情況:大小的三個向量n,以及三個大小向量n/10nn*10分別(這兩種情況給出相同數目的組合)。n最多可達240(選擇此值是為了避免在筆記本電腦中使用虛擬內存)。

結果如下圖所示。這個ndgrid-基于解決方案的持續時間被認為比combvec..值得注意的是,combvec在不同大小的情況下,變化要少一些。



基準代碼

功能ndgrid-基于基礎的解決辦法:

function combs = f1(vectors)n = numel(vectors); %// number of vectorscombs = cell(1,n); %
// pre-define to generate comma-separated list[combs{end:-1:1}] = ndgrid(vectors{end:-1:1}); %
// the reverse order in these two%// comma-separated lists is needed to produce the rows of the result matrix in%
// lexicographical ordercombs = cat(n+1, combs{:}); %// concat the n n-dim arrays along dimension n+1combs = reshape(combs,[],n);

功能combvec解決辦法:

function combs = f2(vectors)combs = combvec(vectors{:}).';

通過調用來測量時間的腳本timeit關于這些職能:

nn = 20:20:240;t1 = [];t2 = [];for n = nn;
    %//vectors = {1:n, 1:n, 1:n};
    vectors = {1:n/10, 1:n, 1:n*10};
    t = timeit(@() f1(vectors));
    t1 = [t1; t];
    t = timeit(@() f2(vectors));
    t2 = [t2; t];end


查看完整回答
反對 回復 2019-06-05
  • 3 回答
  • 0 關注
  • 936 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號