Create a array of 1 and -1

EDITED
Hi there, I want a 4 x 1 vector of all combinations of 1 and -1.
For instance [1 1 1 1] , [1 1 1 -1] , [1 1 -1 1]
etc..

댓글 수: 1

Walter Roberson
Walter Roberson 2012년 5월 4일
[1 1 1 -1] appears to be the same combination as [1 1 -1 1]. Are you sure you want combinations and not permutations?

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 4일

1 개 추천

use function ff2n from Statistics Toolbox
out = ff2n(4)*2-1;
OR:
out = unique(nchoosek((-1).^(1:8),4),'rows');

댓글 수: 2

Dr. Seis
Dr. Seis 2012년 5월 4일
I knew there was a more efficient way to do this!
Andrei Bobrov
Andrei Bobrov 2012년 5월 4일
Thank you Elige.

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 5월 4일

0 개 추천

Taken how many at a time? If you say 2 at a time, then you have the problem that there are only 3 combinations: [-1 -1], [-1 1], [1 1]. The potential combination [1 -1] is the same combination as [-1 1] because by definition the order within a combination is irrelevant.

댓글 수: 2

Dr. Seis
Dr. Seis 2012년 5월 4일
4x1 array... i.e., [1;1;1;1], [1;1;1;-1], [1;1;-1;-1], ...
Mate 2u
Mate 2u 2012년 5월 4일
Wheres the input?

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

Dr. Seis
Dr. Seis 2012년 5월 4일

0 개 추천

Not the most efficient, but:
>> a=nchoosek([1,2,3,4,5,6,7,8],4);
>> b = ones(size(a));
>> b(mod(a,2)==0) = b(mod(a,2)==0)*-1;
>> c = unique(b,'rows')'
c =
Columns 1 through 12
-1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1
-1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1
-1 -1 1 1 -1 -1 1 1 -1 -1 1 1
-1 1 -1 1 -1 1 -1 1 -1 1 -1 1
Columns 13 through 16
1 1 1 1
1 1 1 1
-1 -1 1 1
-1 1 -1 1

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by