How Can I Efficiently Construct a Specific Matrix in MATLAB?

  • Thread starter Thread starter azdang
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary
SUMMARY

The discussion focuses on constructing a specific nxn matrix in MATLAB, where the elements are defined as aij = -1 for i > j, aii = 1, and zero elsewhere. The user successfully creates the matrix using built-in functions: B=eye(n,n) for the identity matrix and C=tril(-ones(n,n),-1) for the lower triangular matrix. The final matrix A is obtained by combining B and C. However, the user seeks a more efficient method that aligns with their teacher's expectations.

PREREQUISITES
  • Familiarity with MATLAB syntax and functions
  • Understanding of matrix operations in MATLAB
  • Knowledge of lower triangular matrices
  • Experience with matrix indexing in MATLAB
NEXT STEPS
  • Research MATLAB's built-in functions for matrix creation
  • Explore efficient matrix manipulation techniques in MATLAB
  • Learn about MATLAB's array operations to optimize performance
  • Investigate the use of logical indexing in MATLAB for matrix construction
USEFUL FOR

Students learning MATLAB, educators teaching matrix operations, and developers seeking to optimize matrix construction in MATLAB.

azdang
Messages
80
Reaction score
0
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!
 
Physics news on Phys.org
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!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K