From: Linda Davis on 13 Jul 2010 15:33 Remedial, I know: I'm "teaching myself MatLab" and I've only had BASIC about a hundred years ago...I've stumbled again. Using David McMahon's book, I'm trying to write a script following his example (and in his style, which I appreciate, he leads you through examples, then sets you up without spoonfeeding you) and I've missed something critical, but cannot figure out my programming gaff. I'm embarrassed to ask for help, but I have to learn this so that I can actually wrestle with real data from satellites soon! So, I was to write a script so that I use a for loop to create "an array with individual elements repeated by frequency:" Given a set of employees with their ages, (e.g., two employees aged 17, one employee aged 18, etc.), I create an array of absolute frequency data. then I set the bin width to 1; and the next step is what I think is not being read when I call the script: "create an array that represents the ages ranged from 17 to 43 with a binwidth of one year". Next, we "collect the raw data" using a "For Loop to sweep through the data as" [I've shown below starting with raw =[] to end] in MatLab's command window then, ave=mean(raw) gives me a number less than 1, whereas the average of the ages should be 30.7308.....and if I just ask it to return the values for raw, I get the array for my f_abs, not the array of all the ages. Would anyone be able to help me see where I have gone wrong? Please. %define the function; creating a singular array; the outline in text is %"gapped" function f_abs=raw f_abs=[2,1,0,0,3,0,0,1,0,1,0,4,0,0,2,0,1,2,0,0,3,0,1,2,0,0,3]; binwidth=1; bins=(17:binwidth:43); raw=[]; for i = 1:length(f_abs) if f_abs(i)>0 new = bins(i)*ones(1,f_abs(i)); else new=[]; end raw=[raw,new]; end -- Linda
From: Wayne King on 13 Jul 2010 17:31 "Linda Davis" <linda.l.davis.removethis(a)jpl.nasa.gov> wrote in message <i1if1i$823$1(a)fred.mathworks.com>... > Remedial, I know: I'm "teaching myself MatLab" and I've only had BASIC about a hundred years ago...I've stumbled again. Using David McMahon's book, I'm trying to write a script following his example (and in his style, which I appreciate, he leads you through examples, then sets you up without spoonfeeding you) and I've missed something critical, but cannot figure out my programming gaff. I'm embarrassed to ask for help, but I have to learn this so that I can actually wrestle with real data from satellites soon! > > So, I was to write a script so that I use a for loop to create "an array with individual elements repeated by frequency:" Given a set of employees with their ages, (e.g., two employees aged 17, one employee aged 18, etc.), I create an array of absolute frequency data. then I set the bin width to 1; and the next step is what I think is not being read when I call the script: "create an array that represents the ages ranged from 17 to 43 with a binwidth of one year". Next, we "collect the raw data" using a "For Loop to sweep through the data as" [I've shown below starting with raw =[] to end] > in MatLab's command window then, ave=mean(raw) gives me a number less than 1, whereas the average of the ages should be 30.7308.....and if I just ask it to return the values for raw, I get the array for my f_abs, not the array of all the ages. > > Would anyone be able to help me see where I have gone wrong? Please. > > > %define the function; creating a singular array; the outline in text is > %"gapped" > function f_abs=raw > f_abs=[2,1,0,0,3,0,0,1,0,1,0,4,0,0,2,0,1,2,0,0,3,0,1,2,0,0,3]; > binwidth=1; > bins=(17:binwidth:43); > raw=[]; > for i = 1:length(f_abs) > if f_abs(i)>0 > new = bins(i)*ones(1,f_abs(i)); > else > new=[]; > end > raw=[raw,new]; > end > > -- > Linda Hi Linda, Welcome to MATLAB!! The problem is in the way you defined your function. Try this: Define a function called employdata. Go file -> new script, then copy and paste the following into the empty script. function Data = employdata(f_abs) binwidth=1; bins=(17:binwidth:43); Data =[]; for i = 1:length(f_abs) if f_abs(i)>0 new = bins(i)*ones(1,f_abs(i)); else new=[]; end Data =[Data,new]; end Save the file as employdata.m in a directory that is in the MATLAB path. Not sure what platform you are using, but I'll assume Windows for a moment. Create a directory called c:\matlabprogs and save employdata.m in there. Then add that directory to MATLAB's path by entering: addpath 'c:\matlabprogs' at the command prompt. Then execute the following (you can just copy and paste): f_abs=[2,1,0,0,3,0,0,1,0,1,0,4,0,0,2,0,1,2,0,0,3,0,1,2,0,0,3]; Data = employdata(f_abs); Now you have a variable Data consisting of 26 measurements in your MATLAB workspace. You can now see that mean(Data) is 30.7308. Enter: hist(Data) to see a histogram of your data. Hope that helps, Wayne
From: Linda Davis on 13 Jul 2010 18:14 Thank you Wayne! I knew the first part was problematic, Linda "Wayne King" <wmkingty(a)gmail.com> wrote in message <i1ilv7$fpj$1(a)fred.mathworks.com>... > "Linda Davis" <linda.l.davis.removethis(a)jpl.nasa.gov> wrote in message <i1if1i$823$1(a)fred.mathworks.com>... > > Remedial, I know: I'm "teaching myself MatLab" and I've only had BASIC about a hundred years ago...I've stumbled again. Using David McMahon's book, I'm trying to write a script following his example (and in his style, which I appreciate, he leads you through examples, then sets you up without spoonfeeding you) and I've missed something critical, but cannot figure out my programming gaff. I'm embarrassed to ask for help, but I have to learn this so that I can actually wrestle with real data from satellites soon! > > > > So, I was to write a script so that I use a for loop to create "an array with individual elements repeated by frequency:" Given a set of employees with their ages, (e.g., two employees aged 17, one employee aged 18, etc.), I create an array of absolute frequency data. then I set the bin width to 1; and the next step is what I think is not being read when I call the script: "create an array that represents the ages ranged from 17 to 43 with a binwidth of one year". Next, we "collect the raw data" using a "For Loop to sweep through the data as" [I've shown below starting with raw =[] to end] > > in MatLab's command window then, ave=mean(raw) gives me a number less than 1, whereas the average of the ages should be 30.7308.....and if I just ask it to return the values for raw, I get the array for my f_abs, not the array of all the ages. > > > > Would anyone be able to help me see where I have gone wrong? Please. > > > > > > %define the function; creating a singular array; the outline in text is > > %"gapped" > > function f_abs=raw > > f_abs=[2,1,0,0,3,0,0,1,0,1,0,4,0,0,2,0,1,2,0,0,3,0,1,2,0,0,3]; > > binwidth=1; > > bins=(17:binwidth:43); > > raw=[]; > > for i = 1:length(f_abs) > > if f_abs(i)>0 > > new = bins(i)*ones(1,f_abs(i)); > > else > > new=[]; > > end > > raw=[raw,new]; > > end > > > > -- > > Linda > > Hi Linda, Welcome to MATLAB!! The problem is in the way you defined your function. Try this: > > Define a function called employdata. Go file -> new script, then copy and paste the following into the empty script. > > function Data = employdata(f_abs) > binwidth=1; > bins=(17:binwidth:43); > Data =[]; > for i = 1:length(f_abs) > if f_abs(i)>0 > new = bins(i)*ones(1,f_abs(i)); > else > new=[]; > end > Data =[Data,new]; > end > > Save the file as employdata.m in a directory that is in the MATLAB path. Not sure what platform you are using, but I'll assume Windows for a moment. Create a directory called c:\matlabprogs and save employdata.m in there. Then add that directory to MATLAB's path by entering: > > addpath 'c:\matlabprogs' > > at the command prompt. Then execute the following (you can just copy and paste): > > f_abs=[2,1,0,0,3,0,0,1,0,1,0,4,0,0,2,0,1,2,0,0,3,0,1,2,0,0,3]; > Data = employdata(f_abs); > > Now you have a variable Data consisting of 26 measurements in your MATLAB workspace. You can now see that > > mean(Data) > > is 30.7308. > > Enter: > > hist(Data) > > to see a histogram of your data. > > Hope that helps, > Wayne
|
Pages: 1 Prev: how to call function from ohter m. file ? Next: Matrix multiplication within a matrix or array |