How to make my working function 1)a for loop and 2) vectorize it?

16 vues (au cours des 30 derniers jours)
rebecca croon
rebecca croon le 2 Mar 2020
Modifié(e) : Guillaume le 2 Mar 2020
Prompt: 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 in 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.2 and 10.4 (inclusive).
Write a function to read in the data file ‘pipe.dat’ and count how many rejects there are. A reject is any piece of pipe that has an invalid weight OR length. The function shall have one input argument, which is the name of the data file. The function shall have one output argument, which shall be the number of rejects. Please implement this function using for- loops.
An example call of the function should look like this:
>> Num = CountRejects('pipe.dat')
Num =
3
(Question): submit two programs with different implementations: one uses for loops as described above, the other uses Matlab vectorization.
Function: (this code works) how do I make this a for loop and vectorize this?:
function [Num] = CountRejects(DataSource)
%Read in the Data
Data=importdata(DataSource);
Num=0;
for i=1:length(Data)
if Data(i,1)<2.1 || Data(i,1)>2.3 || Data(i,2)<10.2 || Data(i,2)>10.4
Num=Num+1;
end
end
end
  5 commentaires
rebecca croon
rebecca croon le 2 Mar 2020
Last time I looked at it, it works. The dats is: 2.24 10.31
2.32 10.36
2.21 10.35
2.12 10.39
2.25 10.41
2.19 10.25
I could be wrong, but im new to this whole matlab
Guillaume
Guillaume le 2 Mar 2020
Modifié(e) : Guillaume le 2 Mar 2020
"The code works, promise"
As I said, test it with a file with just one line to see that it errors (as demonstrated by Matt). Promise!
Then to make it work, replace the length by the appropriate function as I've already explained. The content of pipe.dat is irrelevant, the code will error will all files which have just one line.
As for your question, it's already been answered hence why I didn't address it.
Note: My purpose in pointing out that your code doesn't work is to teach you bug-free code. Your code may well pass the assignment because it's never tested on a file with just one line. Yet, it has an inherent bug due to the use of length on a 2D matrix. Most people don't know how length work on 2D matrices and end up using it where it shouldn't be used. And they keep doing it on important code. The code works fine when they test it, but then on a real file suddenly it errors and they don't understand why. Usually, just an hour before a presentation to an important client with your boss breathing down your back.
Do yourself a favor, don't use length. Use a function that always returns the number of rows, which is size(Data, 1).

Connectez-vous pour commenter.

Réponses (1)

Matt J
Matt J le 2 Mar 2020
Modifié(e) : Matt J le 2 Mar 2020
As a hint, this how you could test, without a loop, which entries in the 2nd column of Data are between 1.5 and 4 (numbers that I made up):
bool = ( 1.5<=Data(:,2) & Data(:,2)<=4 );

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by