Setting custom matrix dimensions

6 次查看(过去 30 天)
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!

采纳的回答

John D'Errico
John D'Errico 2015-3-21
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.
  1 个评论
Hannah Andrews
Hannah Andrews 2015-3-21
Sorry, I should have been more specific in my question. The answer does not need to be a matrix - I just thought that was the only way to do it. The output doesn't need complete rows, as long as there is no more than 10 entries in each.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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