using double for loops for calculating the mean and standard deviation of rows and columns of a matrix in matlab

Here's an example code that calculates the mean and standard deviation of rows and columns of a matrix using double for loops in MATLAB:

main.m
matrix = [1 2 3; 4 5 6; 7 8 9]; % example matrix

% calculating means and standard deviations of rows
for i = 1:size(matrix, 1)
    row = matrix(i,:);
    row_mean = mean(row);
    row_std = std(row);
    disp(['Row ', num2str(i), ' mean: ', num2str(row_mean)]);
    disp(['Row ', num2str(i), ' standard deviation: ', num2str(row_std)]);
end

% calculating means and standard deviations of columns
for j = 1:size(matrix, 2)
    col = matrix(:,j);
    col_mean = mean(col);
    col_std = std(col);
    disp(['Column ', num2str(j), ' mean: ', num2str(col_mean)]);
    disp(['Column ', num2str(j), ' standard deviation: ', num2str(col_std)]);
end
644 chars
20 lines

This script first defines an example matrix, and then uses two for loops to iterate over the rows and columns, respectively. For each row and column, the mean() and std() functions are applied, and the results are printed to the command window using disp(). In the output, the mean and standard deviation of each row and column are displayed.

gistlibby LogSnag