linear combination of vector and permutation
Show older comments
Hi, i' m developing a program where I find myself with this problem that I couldn't solve nem with combntns(v,k) or with nchoosek .
Anyone here to help me?
Let A=[-1,1] and B=3
I need to find all possible combinations taking from B to B vector element A ( It can be with repetition of the vector element )
I have done it manually, but I need it automatically, or a Matlab function does it.
Let A and B the output of this function has to be:
Out=[-1,-1,-1;
-1,-1, 1
-1, 1,-1
-1, 1, 1
1,-1,-1
1,-1, 1
1, 1,-1
1, 1, 1],
Accepted Answer
More Answers (2)
One way:
A=[-1,1];B=3;
[Out{1:B}]=ndgrid(A);
Out=reshape( cat(B+1,Out{:}),[],B)
2 Comments
Fidele Adanvo
on 4 Oct 2022
Stephen23
on 4 Oct 2022
+1 very neat usage of CAT and RESHAPE.
James Tursa
on 4 Oct 2022
Edited: James Tursa
on 4 Oct 2022
Another way very similar to Torsten's method, limited to two desired values:
n = 3;
2*(dec2bin(0:2^n-1)-'0')-1
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!