Setting custom matrix dimensions

6 visualizaciones (últimos 30 días)
Hannah Andrews
Hannah Andrews el 21 de Mzo. de 2015
Comentada: Hannah Andrews el 21 de Mzo. de 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!

Respuesta aceptada

John D'Errico
John D'Errico el 21 de Mzo. de 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.
  1 comentario
Hannah Andrews
Hannah Andrews el 21 de Mzo. de 2015
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by