Take cartesian product between lists of tuples and a vector

16 ビュー (過去 30 日間)
mrule
mrule 2015 年 1 月 30 日
コメント済み: Mike Croucher 2023 年 4 月 4 日
I know matlab doesn't have tuples or vectors, but what I mean is say I have
A = [ 0 1 ; 2 3 ]
B = [ 4 5 ; 6 7 ]
C = [ 0 1 ]
I'd like to take the cartesian product of these items, like the "product" function from itertools in python.
product(A,B,C) =
[ 0 1 4 5 0;
0 1 4 5 1;
0 1 6 7 0;
0 1 6 7 1;
2 3 4 5 0;
2 3 4 5 1;
2 3 6 7 0;
2 3 6 7 1]
Is there a function in matlab that does this, accepting an arbitrary number of arguments? All the examples say "use meshgrid" which doesn't work in this case since that only takes the product of 2 or 3 vectors.

採用された回答

Matt J
Matt J 2015 年 1 月 30 日
編集済み: Matt J 2015 年 1 月 30 日
Use ndgrid.
ma=size(A,1);
mb=size(B,1);
mc=size(C,1);
[a,b,c]=ndgrid(1:ma,1:mb,1:mc);
product = [A(a,:),B(b,:),C(c,:)]
product =
0 1 4 5 0
2 3 4 5 0
0 1 6 7 0
2 3 6 7 0
0 1 4 5 1
2 3 4 5 1
0 1 6 7 1
2 3 6 7 1
  2 件のコメント
mrule
mrule 2015 年 1 月 30 日
編集済み: mrule 2015 年 1 月 30 日
perfect!
Matt J
Matt J 2015 年 1 月 30 日
Great, then please do close the question with

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

その他の回答 (1 件)

saeed oveisi
saeed oveisi 2021 年 12 月 12 日

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by