# How Can I Efficiently Construct a Specific Matrix in MATLAB?

• azdang
In summary, the conversation is about constructing a specific matrix for a program in MATLAB. The individual has found one way to do it using in-built functions but is unsure if it is the most efficient way. They are seeking advice from someone familiar with MATLAB and may also ask their teacher for clarification.
azdang
Hi. I'm not sure if anyone here knows MATLAB, but I'm horrible with it. I have to write a program, which I actually figured out how to do, except that I have to first construct a certain matrix. I found one way to do it, but I'm sure it's not how my teacher wants it.

Let A be the nxn matrix with elements aij = -1 for i > j, aii=ain=1, 1<=i<=n, and zero everywhere else.

The only thing I've been successful with is to do this:

B=eye(n,n);B(1:n,n)=1; (This puts 1's on the diagonal and in the last column)

C=tril(-ones(n,n),-1); (This creates a lower triangular matrix with -1's below the diagonal)

Then, A = B+C.

I am sure there must be a more efficient way to construct this matrix that my teacher is expecting, but I'm not sure what it is. Does anyone who has experience with MATLAB know how I might be able to do this? Thank you!

You could do it with for loops, but you've recognised some in-built functions of MATLAB and taken advantage of them (correctly!). The best person to ask what is required, of course, is your teacher!

## 1. How do I create a matrix in MATLAB?

To create a matrix in MATLAB, you can use the matrix function or manually enter the values using square brackets. For example, A = matrix([1 2 3; 4 5 6; 7 8 9]) will create a 3x3 matrix with the given values.

## 2. How do I perform operations on matrices in MATLAB?

You can use built-in functions such as addition, subtraction, multiplication, and division to perform operations on matrices in MATLAB. You can also use the .* and ./ operators for element-wise operations.

## 3. How can I access specific elements or rows/columns in a matrix?

To access specific elements in a matrix, you can use indexing. For example, A(1,2) will access the element in the first row and second column of matrix A. You can also use A(1,:) to access the entire first row or A(:,2) to access the entire second column.

## 4. How do I resize a matrix in MATLAB?

To resize a matrix in MATLAB, you can use the reshape function. For example, A = reshape(A, 6, 2) will reshape matrix A into a 6x2 matrix. You can also use transpose or .' operator to switch the rows and columns of a matrix.

## 5. Can I save my matrix in MATLAB for future use?

Yes, you can save your matrix in MATLAB using the save function. For example, save('matrixA.mat', 'A') will save matrix A in a file named matrixA.mat for future use. You can also use the load function to load the saved matrix into your workspace.

• Calculus and Beyond Homework Help
Replies
3
Views
619
• Calculus and Beyond Homework Help
Replies
2
Views
444
• Calculus and Beyond Homework Help
Replies
2
Views
2K
Replies
15
Views
1K
• Calculus and Beyond Homework Help
Replies
14
Views
777
• Calculus and Beyond Homework Help
Replies
12
Views
1K
• Calculus and Beyond Homework Help
Replies
2
Views
734
• Calculus and Beyond Homework Help
Replies
3
Views
3K
• Calculus and Beyond Homework Help
Replies
3
Views
1K
• Calculus and Beyond Homework Help
Replies
15
Views
2K