How do i write that matrix?(with zeros,ones,eye)
조회 수: 4 (최근 30일)
이전 댓글 표시
(11111
00011
10000
A= 01000
00010
00001)
댓글 수: 1
Steven Lord
2019년 10월 24일
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
답변 (1개)
Udoi
2022년 6월 22일
The above matrix can be created in a lot of different ways.
An interesting observation for the above matrix which we require to construct is that,it consists of zeroes and ones.So we can first make a matrix of dimensions 6*5,i.e 6 rows and 5 columns,filled iwth zeroes using the command a=zeros(6,5);
X = zeros(sz1,...,szN) returns an sz1-by-...-by-szN array of zeros where sz1,...,szN indicate the size of each dimension. For example, zeros(2,3) returns a 2-by-3 matrix.
After constructing the matrix consisting of zeroes,we can iterate over the rows and columns using two nested for loops and update the cell contents i.e a(i,j) as required.
Code snippet:
a=zeros(6,5);
for i=1:6
for j=1:5
if(i==1)
a(i,j)=1;
end
if(i==2 && (j==4 || j==5))
a(i,j)=1;
end
if(i==3 && j==1)
a(i,j)=1;
end
if(i==4 && j==2)
a(i,j)=1;
end
if(i==5 && j==4)
a(i,j)=1;
end
if(i==6 && j==5)
a(i,j)=1;
end
end
end
Another much simpler way of creating the desired matrix would be to write down the rows separated by semicolons.This is the standard and basic way to create a matrix,but might get tedious if the dimensions of the matrix are large enough.
a = [1 1 1 1 1;
0 0 0 1 1;
1 0 0 0 0;
0 1 0 0 0;
0 0 0 1 0;
0 0 0 0 1
];
If the MATLAB codes written above seem unfamiar to you,starting out with the MATLAB On-ramp tutorials would be a good place to start learning.
Follow the link (https://www.mathworks.com/support/learn-with-matlab-tutorials.html).
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!