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 CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by