Write a working matlab function

  • Thread starter Thread starter tuablink
  • Start date Start date
  • Tags Tags
    Function Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
tuablink
Messages
24
Reaction score
0

Homework Statement


Write a working Matlab function that takes as input a positive integer n and outputs a
2n by 2n matrix whose top left n by n block and bottom right n by n block are zero,
whose top right n by n block is the n by n identity matrix and whose bottom left n by n
block is filled with uniform [0,1] random numbers.


Homework Equations





The Attempt at a Solution


function MatrixMul
n = input('Please enter a positive integer: ');
A(2*n,2*n)=0;
rand(n,n);
eye(n)
display(A)
 
Physics news on Phys.org
That's a start, but it's nowhere near what you want. Your function needs to create four nxn matrices of specific types, and then concatenate (join) them together to create the 2n x 2n matrix specified in your problem. To learn about matrices, see the section titled Working with Matrices in Ch. 2 in the link below. Also see the section titled Concatenation in Ch. 2 in the link below.

Here's a link to the Getting Started documentation for matlab: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf

Comments on your code
A(2*n,2*n) = 0;
Sets one entry in matrix A to 0 and does nothing to any of the other entries. I'm sure that's not what you intended.

rand(n, n);
I believe this will create an nxn matrix of random numbers, but it needs to be an assignment statement to store them in a matrix variable. Here's how to do that.
Random_matrix = rand(n, n);

eye(n)
As above, this statement doesn't store the matrix variable anywhere. You can do it this way.
I_matrix = eye(n);

Now all you have to do is concatenete the n x n matrices that have created into the big 2n x 2n matrix.