I have 4 vectors as shown below. I want to create a matrix that list all the possible combinations with the condition that
x = [0 1 2 3 4 5 6]
x1 = [0 3 6]
x2 = [0 3 6]
x3 = [0 3 6]

 채택된 답변

John D'Errico
John D'Errico 2023년 2월 10일
편집: John D'Errico 2023년 2월 10일

1 개 추천

Easy enough.
x = 0:6;
[x1,x2,x3] = ndgrid([0 3 6]);
x123 = [x1(:),x2(:),x3(:)];
ind = ismember(sum(x123,2),3*x);
x123(ind,:)
ans = 27×3
0 0 0 3 0 0 6 0 0 0 3 0 3 3 0 6 3 0 0 6 0 3 6 0 6 6 0 0 0 3
You want to be careful that you work in INTEGER arithmetic though. This is why I multiplied x by 3, instead of dividing the sum by 3.

댓글 수: 1

Nicolas Leport
Nicolas Leport 2023년 2월 10일
편집: Nicolas Leport 2023년 2월 10일
Thanks, how do I add the corresponding x value to that matrix?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2023년 2월 9일

편집:

2023년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by