how to write code to try every combination of N-dim vector?
Show older comments
Hello, I want to write a code to try every combination of a N-dimentional vector(with entry 0 or 1 or 2). For example, if N=3, all combinations are (0,0,0)(0,0,1)...(2,2,2) and there are 3^N=27.
One brutal way is for commands: for i=0:2->for j=... but I have to write N these for commands, which is not wise. I wonder it there a fast and smart way to try all combinations?
Thanks a lot!
Accepted Answer
More Answers (1)
Daniel Shub
on 24 May 2012
x = fullfact([3,3,3])-1
Categories
Find more on Loops and Conditional Statements 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!