MATLAB: Printing Results out of a for Loop

for loopfunctionminprint

I have Written a Small Function that Returns the Absolute Difference Between the Max and Min Elements of Each Row in a Matrix and Assigns the Results to the output "rows_diff" as Row Vector and Also Gets the Diff. Between the Max and Min of Whole Matrix in "tot_diff"
All what I want to Know is How to Print Out the Results of the for Loop Returning them in "rows_diff" as row vector not just the Last Result as the Case with this Version of the Function:-
function [rows_diff, tot_diff] = minimax(M)
a = size(M,1);
for i = 1:a
rows_diff = abs(max(M(i,:)) – min(M(i,:)));
end
tot_diff = max(M(:)) – min(M(:));

Best Answer

rows_diff = abs(max(M(i,:)) - min(M(i,:)));
This overwrites the value of rows_diff each time. This is ok if you just want to print it out, but to store it (which seems like you want to) you need:
rows_diff(i) = abs(max(M(i,:)) - min(M(i,:)));
to print this value, in your loop place a fprintf
fprintf('Value of rows_diff = %f\n',rows_diff(i))