Write a working matlab function

In summary: You can use the syntax shown in the help documentation on Concatenation in Ch. 2. In summary, the task is to write a Matlab function that takes a positive integer n and creates a 2n x 2n matrix. This matrix should have a top left and bottom right n x n block filled with zeros, a top right n x n block as the identity matrix, and a bottom left n x n block filled with random numbers between 0 and 1. The function should use the rand() function to create the random numbers and the eye() function to create the identity matrix. The constructed matrix should be stored in a variable and output using the display() function. To complete this task, the function should also use
  • #1
tuablink
24
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
  • #2
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.
 

FAQ: Write a working matlab function

1. What is a working matlab function?

A working matlab function is a set of programming instructions written in the Matlab language that can be called upon to perform a specific task or calculation. It is a reusable block of code that helps to streamline the process of writing and executing code.

2. How do I write a working matlab function?

To write a working matlab function, you first need to create a new file in Matlab and save it with a .m extension. Then, you can use the "function" keyword to define the name of your function, input and output variables, and the code that will be executed. Finally, you can call your function from the main Matlab script or command window to test its functionality.

3. What are the advantages of using matlab functions?

There are several advantages of using matlab functions, including:

  • Reusability: Functions can be called multiple times within a program, saving time and effort in writing repetitive code.
  • Modularity: Functions help to break down a larger program into smaller, more manageable parts.
  • Efficiency: By using functions, you can avoid writing the same code multiple times, making your program more efficient.
  • Readability: Functions make the code easier to read and understand, especially for complex programs.

4. Can I pass variables to and from a matlab function?

Yes, you can pass variables to and from a matlab function by defining input and output arguments in the function declaration. Input arguments are used to pass values from the main program to the function, while output arguments are used to return values from the function back to the main program.

5. How do I debug a matlab function?

To debug a matlab function, you can use the built-in debugging tools in Matlab, such as breakpoints, stepping through the code, and inspecting variables. You can also use the "dbstop" command to add breakpoints at specific lines of code in your function. Additionally, you can use the "disp" function to display the values of variables at different points in your code to help identify and fix errors.

Similar threads

Replies
3
Views
1K
Replies
3
Views
2K
Replies
2
Views
1K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
9
Views
6K
Back
Top