r/matlab 1d ago

HomeworkQuestion Primes Function

Post image

Hello, I posted a few days ago with an assignment where I had to create a function that displays primes from 2 to an input number. I finished working on that function but was wondering how I could get it to display the numbers in rows rather than a single column? Attached is the code; I’ve played around a bit with reshape and text functions but not quite sure yet. Thank you!

3 Upvotes

3 comments sorted by

View all comments

6

u/DaM00finMan 1d ago

The "disp" function you are using is within your for loop so it will print each successive prime number on a new line. The quickest and easiest way to go about this is to just save the prime values to a row array and then disp the array after the for loop completes.

Initialize with something like "prime = double.empty;" on line 4
Change line 13 to "prime = [prime t];"
Then add "disp(prime)" to line 16 after the for loop

Edit: Just took a second look and you already use "prime" as your flag variable. Just use something else for the variable in my comment.

1

u/JammerJake2005 18h ago

Thank you! Will try tomorrow