In: Computer Science
Given the matrix as shown below
n_array =
3 1 8
3 5 7
4 9 2
Write a M-file script program that generates this n_array, and answer each question using one line of MATLAB statement.
a) Replace the second column of the n_array with a column of 0s
b) Replace the element in the second-row, third-column with a zero
c) Change the n_array to a 4 x 3 array by adding a row of 0s
The end result for the n_array will be
n_array =
3 0 8
3 0 0
4 0 2
0 0 0
NOTE : PROGRAM IS DONE WITH MATLAB PROGRAMMING LANGUAGE.
HERE AS PER INSTRUCTION OF QUESTION EACH QUESTION (a),b),c)) ARE SOLVED WITH ONE LINE OF MATLAB CODE.
PROGRAM
%generates this n_array
n_array = [3 1 8; 3 5 7; 4 9 2];
fprintf('\nGien Matrix is\n\n')
disp(n_array);
%perform a) b) c) using one line of MATLAB statement.
%a) Replace the second column of the n_array with a column of
0s
n_array(:,2)=0; % after colon(:) 2 signifies 2 nd column
%b) Replace the element in the second-row, third-column with a
zero
n_array(2,3)=0; % row number 2 and column number 3
%c) Change the n_array to a 4 x 3 array by adding a row of
0s
n_array(4,:)=0; %4 before colon(:) signifies full 4th row
%The end result for the n_array will be
fprintf('\nEnd result of n_array is\n\n');
disp(n_array);
SCREN SHOT
OUTPUT