Create Matrix without For Loop in MatLab

In summary, the conversation is discussing creating a tridiagonal matrix without the use of a for loop, specifically using Matlab. The suggested method is to add 3 diagonal matrices, and the code provided demonstrates how to do so. The resulting matrix may have a 2 at the first and last position of the main diagonal, but this can be easily removed. Additionally, the conversation includes a demonstration of a MATLAB script that can display a matrix with the desired pattern.
  • #1
realanony87
9
0
I am trying to create the following Matrix without the use of a for loop.
1 2 1 0 0 0 0 0 0 0 0 0
0 0 1 2 1 0 0 0 0 0 0 0
0 0 0 0 1 2 1 0 0 0 0 0
0 0 0 0 0 0 1 2 1 0 0 0
Etc.
Is there a simple way of doing this

Edit: Using Matlab
 
Last edited:
Physics news on Phys.org
  • #2
Are you using Matlab?
You have a tridiagonal matrix so the easiest way to set it up is to add 3 diagonal matrices.

If you have an m-by-m matrix you can write

diag(2*ones(1,m),0) + diag(ones(m-1,1),1) + diag(ones(m-1,1),-1)


Note that this matrix will have a 2 at the first and last position of the main diagonal, but you can easily remove the first and last row.
 
  • #3
Well It isn't tridiagonal because every row is shifted to the left by one .
 
  • #4
Simply cannot resist the opportunity for me to practise writing a MATLAB script :biggrin:


% will display m by n matrix B that wrapped the elements 1 2 1

m=5; n=10;
A=zeros(1,n);
A(1)=1; A(2)=2; A(3)=1;

for k=1:m
B(k,:)=circshift(A,[1 k-1]);
end
B
 

Related to Create Matrix without For Loop in MatLab

1. How can I create a matrix without using a for loop in MatLab?

To create a matrix without using a for loop in MatLab, you can use the colon operator (:) or the linspace function. The colon operator creates a matrix with evenly spaced values and can be used as follows: matrix = start value:increment:end value. The linspace function creates a matrix with a specified number of evenly spaced values and can be used as follows: matrix = linspace(start value, end value, number of values).

2. What are the advantages of creating a matrix without a for loop in MatLab?

The main advantage of creating a matrix without a for loop in MatLab is that it is more efficient and faster. For loops can be computationally expensive, especially for large matrices. By using the colon operator or linspace function, you can avoid the overhead of a for loop and create a matrix in a more efficient manner.

3. Can I create a complex matrix without using a for loop in MatLab?

Yes, you can create a complex matrix without using a for loop in MatLab. The colon operator and linspace function can be used to create matrices with complex values. For example, you can use the colon operator as follows: matrix = start value:increment:end value + start value*i. The linspace function can also be used with complex values: matrix = linspace(start value, end value, number of values) + linspace(start value, end value, number of values)*i.

4. Are there any limitations to creating a matrix without a for loop in MatLab?

There are some limitations to creating a matrix without a for loop in MatLab. The colon operator and linspace function can only create matrices with evenly spaced values. If you need a matrix with non-uniformly spaced values, you may need to use a for loop or a different approach. Additionally, the colon operator and linspace function may not be suitable for creating very large matrices, as they can use a lot of memory.

5. Can I perform other operations on a matrix created without a for loop in MatLab?

Yes, you can still perform other operations on a matrix created without a for loop in MatLab. The method used to create the matrix does not affect its properties or capabilities. You can perform operations such as addition, subtraction, multiplication, and more on a matrix created without a for loop, just as you would on a matrix created with a for loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
188
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
618
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
138
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
915
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top