151k views
1 vote
Write a function called GenMatBorder that takes an array of numbers and produces an array called OutMat, which replaces the top row, bottom row, left column, and right column of the input array with zeros. Restrictions: Do not use loops.

1 Answer

4 votes

Answer:

clc;

clear all;

function[outMat]=GenMatBorder(inMat)

[row,col]=size(inMat);

frame=ones(row,col);

frame(2:row-1,2:col-1)=0;

inMat(logical(frame))=0;

outMat=inMat;

end

Step-by-step explanation:

  • clc and clear all are used to clear all previous garbage values.
  • A function is declared that gives a matrix named outMat. BenMatBorder is the function name that has a parameter inMat passed as reference.
  • The number of rows and columns from the given matrix are stored to make another matrix frame.
  • frame is another matrix that has same order as given matrix and ha all values equal to 1.
  • Now the values except of border entries(top row, bottom row, left column, and right column ) of frame are set to zero.
  • The logical command is used that says wherever the frame value is true (1) make it equal to 0.
  • So all the border entries (top row, bottom row, left column, and right column ) will become zero stored in inMat
  • Now make outMat = inMat to store the matrix into outMat.
  • end will exit the function.

Now whenever an initialized matrix is given as a parameter to the function GenMatBorder, it will give the output by making border elements zero.

i hope it will help you!

User AlecTMH
by
5.8k points