Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a script (.m file) that creates a matrix filled with zeros or ones. Your m

ID: 3806833 • Letter: W

Question

Write a script (.m file) that creates a matrix filled with zeros or ones. Your matrix must be a minimum size of 25 times 50. There needs to be twice as many columns as rows so that the matrix appears square. Manipulate this matrix using MATLAB commands and loops to create an image or pattern in the matrix. If your initial matrix is zeroes, add ones, and if your initial matrix is ones, add zeros. Your shape cannot be a simple square or a plus sign. It must involve at least four different "lines" or pieces that create the picture Finally, write this image out to a text (.txt) file using file open and printing commands. Remember to include both your (commented) code and the resulting text file image in your lab report.

Explanation / Answer

function WriteToFile(filename, mat)
%open file with write permission
fid = fopen(filename, 'w');
  
for i = 1:size(mat, 1)
for j = 1:size(mat, 2)
%write a line of text
fprintf(fid, '%d ', mat(i, j));
end
fprintf(fid, ' ');
end
  
%close file
fclose(fid);
end

% declare a matrix of size n * m and initialises with 0
mat = zeros(25, 50)

% filling matrix randomly to generate random image
for i = 1:25
x = rand()
for j = 1:50
% making a row 1
if (x < 0.5) % change a 0 to 1
if mat(i, j) == 0
mat(i, j) = mat(i, j) + 1;
end
end
end
% making a column 1
x = rand()
if (x < 0.5)
y = randi(50)
for k = 1: 25
if mat(k, y) == 0
mat(k, y) = mat(k, y) + 1;
end
end
end
end

WriteToFile("image.txt", mat)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote