
% Lab 7 Solutions
% CS 1109, Summer 2011
% July 25

% This script generates a 10x10 matrix populated with random numbers in the
% range [0,1]. It prints out the sum of all the elements in the matrix
% that are greater than 0.5

clc
clear

% Generate the random matrix
randMatrix = rand(10);

% Code to walk the matrix -- the loop index i steps through each row of the
% matrix, and the index j steps through each column
sum = 0;
for i = 1 : 10
    for j = 1 : 10
        % Add the current element to the running sum only if it is greater
        % than 0.5
        if (randMatrix(i, j) > 0.5)
            sum = sum + randMatrix(i, j);
        end
    end
end

fprintf('The sum of the elements > 0.5 is %f\n', sum);


