sophiawilson61 asked . 2020-07-03

how to create an array of all permutations

I have a question about how to create the following array in a simple, concise way (for any permutations).
Think of a 4 digit lock, but numbers allowed are not 0-9, but 1-6 or 0-17 (or any other range).
For 0-9 there are 10^4 permutations, which should be a 10000 x 4 array, each row showing one of the permutations.
 
I thought of building the array from 4 vectors (thousand, hundred, tens, ones), but that is also cumbersome when looking at a 7digit lock with 11 possible numbers for each digit.
example:
4 digits, possible numbers for each digit 1 - 6 (1296 permutations)
unity_ones = [1;6];
which means that the ones vector can be created by:
 
n = 1296/6;
ones_vector = [];
for i=1:n
ones_vector = [ones_vector,ones];
end
 
Then one can do the same for the tens_vector, hundreds_vector and thousands_vector... This seems not very efficient. I am sure there is an easier, one or two liner out there...

matlab, array

Expert Answer

Kshitij Singh answered . 2024-12-21 17:14:48

If you want all of them in an array (which might be too large if the number of digits is too large), you can use

n = number of digits
b = range of the digits (0 to b)
result = dec2base(0:(b+1)^n-1,b+1);

This will give you a character array since the digits might be bigger than 9.

If b is 9 or less, you can turn this into numeric with

result = dec2base(0:(b+1)^n-1,b+1) - '0';

Otherwise, you would need code to convert the letters 'A' - whatever into numbers.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.