All possible combination based on 2^n but with 1 and -1
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi all, thank you for those of you who have answered my question below.
I have a slightly different question but still does not know how to achieve this.
Again, I want to create a matrix containing all possible combination. Example of this is shown below. The size of the matrix depends on the number of variable n and the total of combination should follow the . The example below is valid for
and, hence, the total number of rows is 8. The value of each element is 1 and -1.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/617493/image.png)
How to create this matrix automatically depending on the number of variable n?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/617498/image.png)
댓글 수: 2
답변 (3개)
Dyuman Joshi
2021년 5월 14일
y=dec2bin([7 4 2 1])-'0';
y(y==0)=-1;
z =[y; flipud(y)]
This only works for this particular example. If you want a generalised answer, give more examples.
댓글 수: 2
Dyuman Joshi
2021년 5월 14일
Because only this combination corresponds to the desired result.
That's why I mentioned - "This only works for this particular example. If you want a generalised answer, give more examples"
Daniel Pollard
2021년 5월 14일
You could take the answer from your previous question, subtract 0.5 and multiply by 2. Your accepted answer was
n = 3;
m = dec2bin(0:pow2(n)-1)-'0' % limited precision
which then becomes
n = 3;
m = dec2bin(0:pow2(n)-1)-'0'; % limited precision
m = 2*(m-0.5)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!