) The dlmwrite functions allows us to easily write matrices to files. dlmwrite d
ID: 3759014 • Letter: #
Question
) The dlmwrite functions allows us to easily write matrices to files.
dlmwrite documentation: http://www.mathworks.com/help/matlab/ref/dlmwrite.html
a) Assume we have a matrix, M. Using the dlmwrite function, write the MATLAB code to write the matrix to a file named “myfile.txt”, and have the content of the matrix be separated by commas.
b) What would you change if you wanted the matrix elements to be separated by a space instead of a comma?
2) Practice for loops by iterating through matrices, complete the following sections:
a) Write the MATLAB code to create a 5x5 matrix of zeroes. Store it in a variable named M.
b) Write the MATLAB code to change the diagonals of M from 0’s to 1’s (Hint: Draw the matrix out if you need to visually see this).
Explanation / Answer
---------------Answer for question 1---------------
M = magic(5);
disp(M)
% the content of the matrix be separated by commas.
dlmwrite('myFile.txt',M, ',');
disp("File contents: seperated by comma")
type('myFile.txt')
% the matrix elements to be separated by a space instead of a comma?
dlmwrite('myFileSpace.txt',M, ' ');
disp("File contents - seperated by space")
type('myFileSpace.txt')
--------------output---------------------
Executing the program....
$octave -qf --no-window-system main.m
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.