Skip to main content

Section 3.3 User input

When running numerical experiments like those in Section 3.2, it is inconvenient to edit the script every time we want to change the numbers in it, like the number of rows/columns of a matrix. One can use user input instead, making the script interactive:

m = input('Number of rows: ');
n = input('Number of columns: ');

The text in input command will be shown to the user as a prompt. When the user enters a number, that number gets assigned to the variable m. Same happens with n. After that the computation proceeds.

Modify Example 3.2.1 so that instead of fixed numbers 10 and 10000, it takes user input for those numbers.

Answer
m = input('How many numbers in a sum? ');
n = input('How many sums to compute? ');
A = rand(m, n);
S = sum(A, 1);
histogram(S, 30)