% Kathy Ossman - Suggested Lecture 10 Exercises  - 3 November 2013

% Counting 7s in a vector
clear all;
N = input('How many entries in the vector?   ');
Vector = randi([4 10],[1 N]);
count = 0;
for k = 1:length(Vector)
    if Vector(k) == 7
        count = count + 1;
    end
end
fprintf('The number of 7s in the vector is:  %i  \n',count)

% Counting 7s and Displaying Locations
clear all;
N = input('How many entries in the vector?   ');
Vector = randi([4 10],[1 N]);
count = 0;
for k = 1:length(Vector)
    if Vector(k) == 7
        count = count + 1;
        Location(count) = k;
    end
end
fprintf('The number of 7s in the vector is:  %i  \n',count)
if count > 0
fprintf('The 7s are located in entries:  \n')
disp(Location);
end

% Counting Number of 7s in a matrix and Displaying row/col
% location
clear all;
Matrix = randi([4 10],[6 5]);
count = 0;
for r = 1:6
    for c = 1:5
        if Matrix(r,c) == 7
            count = count + 1;
            Row_Location(count) = r;
            Col_Location(count)= c;
        end
    end
end
disp('Matrix = '); disp(Matrix);
fprintf('The number of 7s in the matrix is:  %i  \n',count)
if count > 0
fprintf('The 7s are located in rows/cols:  \n')
disp([Row_Location' Col_Location']);
end

% Sum the postive entries and the negative entries in a matrix:
clear all;
Matrix = randi([-5 5],[3 4]);  
PositiveSum = 0;
NegativeSum = 0;
for r = 1:3
    for c = 1:4
        if Matrix(r,c) >= 0
            PositiveSum = PositiveSum + Matrix(r,c);
        else
            NegativeSum = NegativeSum + Matrix(r,c);
        end
    end
end
disp('Matrix = '); disp(Matrix);
fprintf('The Positive Sum is:  %i  \n',PositiveSum);
fprintf('The Negative Sum is:  %i   \n', NegativeSum);

% Time Test
clear all;
Vector = randi([4 10],[1 10000]);  % 1000 Entries
tic  % Start Timing My Code
count = 0;
for k = 1:length(Vector)
    if Vector(k) == 7
        count = count + 1;
        Location(count) = k;
    end
end
fprintf('The number of 7s in the vector is:  %i  \n',count)
fprintf('The 7s are located in entries:  \n')
%disp(Location);
MyTime = toc;
disp('My Time = '); disp(MyTime);

tic % Start Timing MATLAB code
Location = find(Vector == 7);
fprintf('The number of 7s in the vector is:  %i  \n',length(Location));
fprintf('The 7s are located in entries:  \n')
%disp(Location);
MATLAB_Time = toc;
disp('MATLAB Time = '); disp(MATLAB_Time);
