problem2 . The dimensions of the matrix are entered by the user Use a MATLAB fun
ID: 2293714 • Letter: P
Question
problem2
. The dimensions of the matrix are entered by the user Use a MATLAB function or a MATLAB script Your code should work in general. . Example: 0 1 0 1 0 Problem2 Generate any square matrix of even dimesions (4x4, 6x6, 8x8, etc) with the following pattern of zeros and ones, which looks like the letter Z. . Vectorize the code as much as possible. .Demonstrate your code to the instructor before leaving the exam. . The dimensions of the matrix are entered by the user . Use a MATLAB function or a MATLAB script. .Your code should work in general. Example: 0 0 0 0 1 0Explanation / Answer
clc;
clear all;
close all;
m=input('enter the value of rows :');
n=input('enter the values of columns:');
if m==n
z=mod(m,2);
if z==0
a=zeros(m,n);
% first row and last row have to be filled with ones
for n1=1:n
a(1,n1)=1;
a(m,n1)=1;
end
% since first row and last row are filled with ones
%remaining have to be filled
%remove 2 from total number of rows for the remaining number
k=m-2;
for i=1:k
a(1+i,n-i)=1;
end
disp(a);
else
disp('the entered matrix is not even');
end
else
disp('the matrix is not square');
end
result :
enter the value of rows :6
enter the values of columns:6
1 1 1 1 1 1
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 0 0 0
1 1 1 1 1 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.