Setting custom matrix dimensions

조회 수: 5 (최근 30일)
Hannah Andrews
Hannah Andrews 2015년 3월 21일
댓글: Hannah Andrews 2015년 3월 21일
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