Setting custom matrix dimensions
2 views (last 30 days)
Show older comments
Hannah Andrews
on 21 Mar 2015
Commented: Hannah Andrews
on 21 Mar 2015
I have created a program which filters out and displays all prime numbers between one and a given input value. I am wondering how I can sort these into a matrix with a maximum of 10 columns with the number of rows subject to change depending on the number that has been entered. Thanks in advance!
0 Comments
Accepted Answer
John D'Errico
on 21 Mar 2015
I assume you want to use this to display your result.
Suppose you find exactly 13 prime numbers in your set? So you have found the first 13 prime numbers? Now, you want to rearrange them into a matrix, with at most 10 columns.
Matrices in MATLAB are rectangular. So the only way you can do that is if the matrix has 13 rows, and 1 column, because 13 is itself prime.
So the answer, ASSUMING you will require a rectangular array, is that you need to find the largest factor of the number of elements that does not exceed 10. This will be the number of columns. That factor need not be a prime factor. The number of columns may be 1.
IF you do not require the result to be rectangular, then the answer is trivial if the last row need not be full. Just remember that this does not comprise a matrix in MATLAB.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!