for loops for matrix standard deviation in matlab

To calculate the standard deviation of each column in a matrix using for loops in MATLAB, you can follow the code below:

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

% Get the number of columns in A
n = size(A, 2);

% Create a vector to store the standard deviations
std_devs = zeros(1, n);

% Loop over each column of A
for i = 1:n
    % Calculate the standard deviation of the ith column
    col_std = std(A(:, i));
    
    % Store the result in the std_devs vector
    std_devs(i) = col_std;
end

% Print the resulting vector of standard deviations
disp(std_devs);
451 chars
21 lines

In the code above, we first define a matrix A and get the number of columns in A. We then create a vector std_devs to store the resulting standard deviations. We loop over each column of A and calculate the standard deviation of that column using the std function. We store the result in the std_devs vector and then print the resulting vector of standard deviations.

gistlibby LogSnag