Create 121x121 Matrix in MATLAB with 0s, 1s and -4s

In summary, the goal is to create a 121x121 matrix in MATLAB with 40 rows containing the sequence 0 1 0 0 1 -4 1 0 0 1, with the sequence being pushed one step to the right in each row. The remaining rows will have 0's and one 1 per row, with the pattern repeating 9 times before returning to the initial 12 rows. The solution involves using a loop to fill the matrix with the desired sequences.
  • #1
monsmatglad
76
0
I need to create a 121*121 matrix in matlab. Forty of the rows have to contain the sequence 0 1 0 0 1 -4 1 0 0 1. and the rest of the rows will have 0's and "one 1" per row. The sequence has to be pushed one step to the right from row 1 to 121, the result should be a matrix with only -4's and 1's on the diaogonal.

The first 12 rows will have the form:
1 0 0 0 0 0 0 0 0 0 0...
0 1 0 0 0 0 0 0 0 0 0...
0 0 1 0 0 0 0 0 0 0 0...

Then the rows with the -4's appear 9 times being separated by only two rows of type 1 and 0.
example:
1 0 0 0 0 0 0 0 0 0 0...
0 1 0 0 0 0 0 0 0 0 0...
0 0 1 0 0 0 0 0 0 0 0...
11 rows
then
9 rows of
0 1 0 0 1 -4 1 0 0 1 0 0 0 0 0 0 0 0 0...
0 0 1 0 0 1 -4 1 0 0 1 0 0 0 0 0 0 0 ...
0 0 0 1 0 0 1 -4 1 0 0 1 0 0 0 ...
after the 9 rows then two of only 1 and 0 again. and it continues like that for 110 rows down, with the remaining being like the twelve first with only 1 and zeros.

phew! Hope this is possible to understand. I have very little experience with programing, so I find this really hard.
2. Homework Equations
please ask for expanation if you don't understand.

3. The Attempt at a Solution

Mons
Reply With Quote
 
Physics news on Phys.org
  • #2
Matrix = zeros(121); % creates a 121x121 matrix of zerosfor i=1:12 % loop to fill the first 12 rows with the sequence 1 0 0 0 0 0 0 0 0 0 0... Matrix(i,i)=1;endfor i=13:110 % loop to fill the next 98 rows with the sequence 0 1 0 0 1 -4 1 0 0 1 0 0 0 0 0 0 0 0 ... Matrix(i,i-1) = 1; Matrix(i,i) = 0; Matrix(i,i+1) = 1; Matrix(i,i+4) = -4; Matrix(i,i+5) = 1; Matrix(i,i+8) = 1;endfor i=111:121 % loop to fill the last 11 rows with the sequence 0 0 1 0 0 0 0 0 0 0 0 ... Matrix(i,i)=1;end
 

1. What is a matrix in MATLAB?

A matrix in MATLAB is a two-dimensional array of numbers or variables arranged in rows and columns. It is a fundamental data structure used in many scientific and engineering applications.

2. How do I create a 121x121 matrix in MATLAB with 0s, 1s and -4s?

To create a 121x121 matrix in MATLAB with 0s, 1s and -4s, you can use the "zeros", "ones" and "repmat" functions. First, use the "zeros" function to create a matrix of 0s with dimensions 121x121. Then, use the "ones" function to create a matrix of 1s with dimensions 121x121. Finally, use the "repmat" function to repeat the matrix of -4s with dimensions 121x121. Add these three matrices together to create the desired matrix.

3. Can I create a matrix with a mix of numbers and characters in MATLAB?

Yes, you can create a matrix with a mix of numbers and characters in MATLAB. However, all elements in a matrix must be of the same data type, so you will need to convert characters to numbers using the "char" function before creating the matrix.

4. How do I access and modify elements in a matrix in MATLAB?

To access and modify elements in a matrix in MATLAB, you can use indexing. Matrix elements are indexed by their row and column numbers. For example, to access the element in the 3rd row and 5th column of a matrix A, you would use A(3,5). To modify an element, you can use the same indexing and assignment operators.

5. Can I perform mathematical operations on matrices in MATLAB?

Yes, you can perform mathematical operations on matrices in MATLAB. MATLAB has built-in functions for basic arithmetic operations (addition, subtraction, multiplication, division) as well as more advanced operations such as matrix multiplication and matrix inversion. You can also create your own custom functions for specific mathematical operations on matrices.

Similar threads

  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
631
  • Calculus and Beyond Homework Help
Replies
5
Views
12K
  • Calculus and Beyond Homework Help
Replies
2
Views
824
  • Calculus and Beyond Homework Help
Replies
10
Views
3K
  • Calculus and Beyond Homework Help
Replies
26
Views
6K
Replies
4
Views
1K
Replies
1
Views
710
  • Calculus and Beyond Homework Help
Replies
32
Views
2K
  • Calculus and Beyond Homework Help
Replies
9
Views
1K
Back
Top