Write a working matlab function

  • Thread starter Thread starter tuablink
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary
SUMMARY

The discussion focuses on creating a Matlab function that generates a 2n by 2n matrix based on specific requirements. The top left and bottom right blocks must be zero, the top right block should be an identity matrix, and the bottom left block filled with uniform random numbers. Key corrections include properly assigning the random matrix and identity matrix to variables and concatenating these matrices to form the final output. The provided code snippets require adjustments to achieve the desired functionality.

PREREQUISITES
  • Understanding of Matlab syntax and functions
  • Knowledge of matrix operations in Matlab
  • Familiarity with random number generation in Matlab
  • Concept of matrix concatenation in Matlab
NEXT STEPS
  • Learn about Matlab matrix operations and indexing
  • Explore the Matlab documentation on random number generation
  • Study the concept of matrix concatenation in Matlab
  • Review examples of creating and manipulating matrices in Matlab
USEFUL FOR

Students, educators, and developers working with Matlab who need to understand matrix manipulation and function creation for homework or projects.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
49
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K