A machine cuts N pieces of a pipe. After each cut, each piece of
pipe is weighed and its length is measured; these 2 values are then
stored in a file called pipe.dat (first the weight and then the length on
each line of the file). Ignoring units, the weight is supposed to be
between 2.1 and 2.3, inclusive, and the length is supposed to be
between 10.3 and 10.4, inclusive. The following is just the beginning
of what will be a long script to work with these data. For now, the
script will just count how many rejects there are. A reject is any piece
of pipe that has an invalid weight and/or length. For a simple example,
if N is 3 (meaning three lines in the file) and the file stores:
2.14 10.30
2.32 10.36
2.20 10.35
there is only one reject, the second one, as it weighs too much. The
script would print:
There were 1 rejects.
Ch5Ex12.m
% Counts pipe rejects. Ignoring units, each pipe should be
% between 2.1 and 2.3 in weight and between 10.3 and 10.4
% in length
% read the pipe data and separate into vectors
load pipe.dat
weights = pipe(:,1);
lengths = pipe(:,2);
N = length(weights);
% the programming method of counting
count = 0;
for i=1:N
if weights(i) < 2.1 || weights(i) > 2.3 || ...
lengths(i) < 10.3 || lengths(i) > 10.4
count = count + 1;
end
end
fprintf('There were %d rejects.\n', count)
need an explanation for this answer? contact us directly to get an explanation for this answer