for loop for calculating the mean and standard deviation of rows and columns of a matrix in matlab

To calculate the mean and standard deviation of rows and columns of a matrix in Matlab, you can use a for loop along with the mean and std functions. Here's an example code that shows how to do it:

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

% Mean and std of rows
for i = 1:size(A, 1)
    row_mean(i) = mean(A(i,:));
    row_std(i) = std(A(i,:));
end
disp("Mean of rows:")
disp(row_mean)
disp("Standard deviation of rows:")
disp(row_std)

% Mean and std of columns
for j = 1:size(A, 2)
    col_mean(j) = mean(A(:,j));
    col_std(j) = std(A(:,j));
end
disp("Mean of columns:")
disp(col_mean)
disp("Standard deviation of columns:")
disp(col_std)
449 chars
23 lines

In this code, we first define a matrix A. Then, we use two for loops to calculate the mean and standard deviation of rows and columns, respectively. In each iteration of the loop, we use the mean and std functions to calculate the mean and standard deviation of the corresponding row or column. Finally, we display the results using the disp function.

gistlibby LogSnag