How would I write a script that will print the following? Up to n. where the user inputs n?
1
2 4
3 6 9
4 8 12 16
5 10 15 25 30
n......
How would I write a script that will print the following? Up to n. where the user inputs n?
1
2 4
3 6 9
4 8 12 16
5 10 15 25 30
n......
It seems like you already figured it out. So, it's time to introduce a few ways to do this that are more MATLABy.
As you have figured out by now, tril
can be used to obtain only the lower triangular part of a matrix. Knowing that, the question is really only: How to make a multiplication table in MATLAB.
First, to get input from the user, use... input
:
n = input('Select a value for n: ') % Please use a space at the end of the string.
% "Select a value for n:10" looks horrible (IMO).
Let's have a look at a few options:
The naive loop approach:
A = zeros(n) % Create a nxn matrix. You should never let matrices "grow"
% inside loops, so allocate the memory first and substitute the
% values afterwards.
for ii = 1:n % Note that I used ii, not i as the variable, see note below explaining why
for jj = 1:n
A(ii,jj) = ii*jj;
end
end
Doesn't really look that nice does it?
The not so naive loop approach:
A = zeros(n);
for ii = 1:n
A(ii,:) = (1:n)*ii;
end
How about using some built-in MATLAB functions? meshgrid
creates a rectangular mesh of values. cumsum
takes the cumulative sum of each column (if the input is a matrix, and the dimension is not specified). So, by combining the two you can get the multiplication table quite simply:
meshgrid(1:4)
ans =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
A = cumsum(meshgrid(1:n));
A =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Now, is this the most MATLABy way to do this? It's not half-bad actually, but there are other alternatives.
You can multiply a horizontal vector with values 1:n
by a vertical vector with the same values to get a multiplication table:
(1:n)'*(1:n)
ans =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
You can use everybody's favorite function bxsfun
, although that's verbose in this case:
A = bsxfun(@times, 1:n, (1:n)')
A =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
And of course, with all of the solutions above:
tril(A)
ans =
1 0 0 0
2 4 0 0
3 6 9 0
4 8 12 16
In summary, a bunch of opportunities, where one of them only requires 18 characters:
tril((1:n)'*(1:n))
* i and j are used to denote the imaginary variable in MATLAB.
As this looks like a homework question, I can give you some hints. In no way is this the fastest or most elegant solution, but it may help you with your construction.
First, given n, work out the size of your matrix, say A. Write A = zeros(#rows,#columns)
.
Second, identify a pattern. The first column is [1,1,3,4,5,6,7,8,9,...,n]
You should be able to work out a way to complete this column.
Now observe that the second column of A is 2x the first column of A. The third column of A is 3x the first column of A. The pattern continues. You can do a forloop to complete the remaining columns.
Last, use the command tril(A)
to take the lower triangular part of matrix A.