Ask your own question, for FREE!
Mathematics 10 Online
OpenStudy (raffle_snaffle):

Matlab help

OpenStudy (raffle_snaffle):

@jim_thompson5910

OpenStudy (raffle_snaffle):

clc ; clear % clears command and workspace window load('weather_data'); % loads data month = 1; % defined variable m = weatherdata(month,:); % extract months data m_avg = mean(m) % average rain fall per day in inches m_max = max(m) % max. rain fall per day in inches d_max = find(m == m_max) % days in which max. rain fell in inches d_zero = find(m == 0) % days in which zero inches fell per day nd_zero = length(d_zero) % number of days in which zero inches fell per day d_nonzero = find(m>0) % days in which at least some rain fell nd_nonzero = length(d_nonzero) % number of days in which at least some rain fell m_avg_nonzero = mean(m(d_nonzero)) % average inches per day of only days in which some rain fell

OpenStudy (raffle_snaffle):

OpenStudy (raffle_snaffle):

@jim_thompson5910 same assignment. I am on the last section.

jimthompson5910 (jim_thompson5910):

how does the script run so far?

OpenStudy (raffle_snaffle):

Test all of your values for the month of January manually to make sure they are correct. Now, improve your script to handle the non-day data points. That is, set the month variable to 2 (February), which has multiple slots with value -99999, and make your script ignore any such values. Note, this is not the same as setting -99999 to zero, as was done in the example. This would invalidate any averages computed for such a month. Run your improved script for the February data and check the results against manual calcula- tions for that month. You may wish to test your script against other months' data. When you are convinced your results and variable names are correct, go through and use the semicolon to supress all screen output, then submit your lab02.m le via D2L to the Lab 2 assignment folder.

OpenStudy (raffle_snaffle):

it runs good

jimthompson5910 (jim_thompson5910):

ok great

jimthompson5910 (jim_thompson5910):

`Test all of your values for the month of January manually to make sure they are correct` so they want you to compute the mean, median, max, etc without using matlab. So use another calculator or you can do it by hand if you really want to take the long route

jimthompson5910 (jim_thompson5910):

the good news is that you'll only focus on row 1

OpenStudy (raffle_snaffle):

give me a sec. I will use the stat function in my 89

jimthompson5910 (jim_thompson5910):

The reason why I'm having you use another calculator is because when they say "test to make sure it's correct", they're implying that matlab may be incorrect. Ideally matlab is error free, but it doesn't hurt to check with another calculator.

OpenStudy (raffle_snaffle):

so the mean matched.

jimthompson5910 (jim_thompson5910):

that's good

OpenStudy (raffle_snaffle):

m_avg = 27.3548 m_max = 272 d_max = 3 d_zero = Columns 1 through 16 1 2 4 5 6 7 11 12 13 16 17 20 21 25 26 27 Columns 17 through 18 28 29 nd_zero = 18 d_nonzero = 3 8 9 10 14 15 18 19 22 23 24 30 31 nd_nonzero = 13 m_avg_nonzero = 65.2308

OpenStudy (raffle_snaffle):

however the max. don't match calculator is 224 and matlab is 272

OpenStudy (raffle_snaffle):

matlab has decimal points after the whole number but in my calculator it only shows 27

OpenStudy (raffle_snaffle):

and I only input the values on first row in weatherdata matrix

jimthompson5910 (jim_thompson5910):

hmm let me think

OpenStudy (raffle_snaffle):

nd_nonzero = length(d_nonzero) % number of days in which at least some rain fell m_avg_nonzero = mean(m(d_nonzero)) % average inches per day of only days in which some rain fell

OpenStudy (raffle_snaffle):

these two lines of code are correct?

jimthompson5910 (jim_thompson5910):

272 is definitely in row 1, column 3 so that's definitely larger than 224

jimthompson5910 (jim_thompson5910):

`d_nonzero = find(m>0)` will look though matrix m and will return the indices where the elements are nonzero so that's why the first element of d_nonzero is 3 because element 3 is the first nonzero element in the first row

jimthompson5910 (jim_thompson5910):

length(d_nonzero) will basically count how many elements are in the matrix d_nonzero so assigning that to nd_nonzero is valid

jimthompson5910 (jim_thompson5910):

`m(d_nonzero)` will spit out the elements in m that are nonzero. What is happening is that you input the nonzero indices (stored in d_nonzero matrix) into matrix m and the output is what you see if you kicked out the nonzero elements in row 1 taking the mean of that will give the avg of the nonzero elements so that line looks valid too

jimthompson5910 (jim_thompson5910):

the error is somewhere in your calculator (probably in the input) because your TI89 said that the max was 224 when 272 is actually the max

OpenStudy (raffle_snaffle):

kk let me look at it again

OpenStudy (raffle_snaffle):

Okay i must have missed a few numbers. I get a max of 272 and an average of 27.258

jimthompson5910 (jim_thompson5910):

hmm matlab is saying 27.3548

OpenStudy (raffle_snaffle):

whoa yeah calculator is saying 27.258

jimthompson5910 (jim_thompson5910):

excel is saying 27.35483871

jimthompson5910 (jim_thompson5910):

btw horizontal scrolling is a pain so I prefer the original 31x12 matrix to avoid horizontal scrolling. Plus it's easier to access all the data

OpenStudy (raffle_snaffle):

Now, improve your script to handle the non-day data points. That is, set the month variable to 2 (February), which has multiple slots with value -99999, and make your script ignore any such values. Note, this is not the same as setting -99999 to zero, as was done in the example. This would invalidate any averages computed for such a month.

OpenStudy (raffle_snaffle):

in the example in the textbook it said to change -99999 to 0's which I did. Do I need to go back and chaange these values to -99999

OpenStudy (raffle_snaffle):

jimthompson5910 (jim_thompson5910):

if it's already done, there's no sense in redoing it. It seems like tedious busy work. But your teacher may want you to show how to do it anyway. I'm not sure how s/he operates

OpenStudy (raffle_snaffle):

so now I need to set month = 2;

jimthompson5910 (jim_thompson5910):

I guess you would design the script to handle cases IF there were -9999 there so I'd just put it in but I wouldn't change the values in the spreadsheet to -9999. That seems like overkill in my opinion of course, you could easily do a simple search/replace to save time

jimthompson5910 (jim_thompson5910):

I'd do it like this `d_negative = find(m == -9999) ` `m(d_negative) = 0` that should do two things 1) the first line in the code above will go through vector m and find all the -9999 values. The locations are stored in vector d_negative 2) the next line will assign the elements that are -9999 to 0. I haven't tested it though so this method may not work. If not, then you'll have to use a for loop to go through each index one by one

OpenStudy (raffle_snaffle):

why is it m() the m on the outside of the parenthesis? Is that a command?

jimthompson5910 (jim_thompson5910):

it's used to access values in the matrix/vector simple uses are.... m(1) returns the first value in vector m m(2) returns the second value in vector m and so on... more complicated uses are... m([1 3 5]) will return the 1st, 3rd, 5th value in vector m m([2 25 312]) will return the 2nd, the 25th, and the 312th element in matrix m so when we say m(d_negative) keep in mind that d_negative is a matrix. It's a matrix of indices

OpenStudy (raffle_snaffle):

month = 2; % defined variable d_negative = find(m == -99999); m(d_negative) = 0 m = Columns 1 through 16 0 0 272 0 0 0 0 9 26 1 0 0 0 50 85 0 Columns 17 through 31 0 90 9 0 0 1 15 224 0 0 0 0 0 33 33

jimthompson5910 (jim_thompson5910):

let's say d_negative = [1 5 8 12] saying m(d_negative) will return the 1st, 5th, 8th and 12th elements in vector m

jimthompson5910 (jim_thompson5910):

I want you to try something try assigning all the 0 values to something else, like 9999 just to see it in action

OpenStudy (raffle_snaffle):

m_avg = 27.3548 m_max = 272 d_max = 3 d_zero = Empty matrix: 1-by-0 nd_zero = 0 d_nonzero = Columns 1 through 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Columns 17 through 31 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 nd_nonzero = 31 m_avg_nonzero = 27.3548 m = Columns 1 through 16 0 0 272 0 0 0 0 9 26 1 0 0 0 50 85 0 Columns 17 through 31 0 90 9 0 0 1 15 224 0 0 0 0 0 33 33 >> m = Columns 1 through 16 0 0 272 0 0 0 0 9 26 1 0 0 0 50 85 0 Columns 17 through 31 0 90 9 0 0 1 15 224 0 0 0 0 0 33 33

jimthompson5910 (jim_thompson5910):

try this bit of code and show me what the output is `d_negative = find(m == 0) ` `m(d_negative) = 9999` you can delete this section later

OpenStudy (raffle_snaffle):

m = Columns 1 through 8 -99999 -99999 272 -99999 -99999 -99999 -99999 9 Columns 9 through 16 26 1 -99999 -99999 -99999 50 85 -99999 Columns 17 through 24 -99999 90 9 -99999 -99999 1 15 224 Columns 25 through 31 -99999 -99999 -99999 -99999 -99999 33 33

jimthompson5910 (jim_thompson5910):

ok so you used -9999 instead of 9999 ? it looks like that bit of code works. You can delete it now. It was just to test it out

OpenStudy (raffle_snaffle):

clc ; clear % clears command and workspace window load('weather_data'); % loads data month = 1; % defined variable m = weatherdata(month,:); % extract months data m_avg = mean(m) % average rain fall per day in inches m_max = max(m) % max. rain fall per day in inches d_max = find(m == m_max) % days in which max. rain fell in inches d_zero = find(m == 0) % days in which zero inches fell per day nd_zero = length(d_zero) % number of days in which zero inches fell per day d_nonzero = find(m > 0) % days in which at least some rain fell nd_nonzero = length(d_nonzero) % number of days in which at least some rain fell m_avg_nonzero = mean(m(d_nonzero)) % average inches per day of only days in which some rain fell month = 2; % defined variable d_negative = find(m == -9999); m(d_negative) = 0

OpenStudy (raffle_snaffle):

so I have to suppress all screen outputs. Why though?

OpenStudy (raffle_snaffle):

clc ; clear % clears command and workspace window load('weather_data'); % loads data month = 1; % defined variable m = weatherdata(month,:); % extract months data m_avg = mean(m); % average rain fall per day in inches m_max = max(m); % max. rain fall per day in inches d_max = find(m == m_max); % days in which max. rain fell in inches d_zero = find(m == 0); % days in which zero inches fell per day nd_zero = length(d_zero); % number of days in which zero inches fell per day d_nonzero = find(m > 0); % days in which at least some rain fell nd_nonzero = length(d_nonzero); % number of days in which at least some rain fell m_avg_nonzero = mean(m(d_nonzero)); % average inches per day of only days in which some rain fell month = 2; % defined variable d_negative = find(m == -9999); m(d_negative) = 0;

jimthompson5910 (jim_thompson5910):

because the command window gets cluttered up with extra data if you don't suppress output. Usually the person reading the report doesn't care how the data was computed. They usually want the final result(s)

jimthompson5910 (jim_thompson5910):

if you're dealing with huge matrices, it's a good idea to suppress output otherwise it gets very cluttered

OpenStudy (raffle_snaffle):

okay sounds reasonable. I got docked points for not doing that on the last lab assignment

jimthompson5910 (jim_thompson5910):

your code looks good. I'm assuming at this point you've fully tested everything a few times. If so, then it looks good enough to be submitted to your teacher

OpenStudy (raffle_snaffle):

yeah I will probably save it and turn it in tomorrow. I may just look at once more before submitting it. Its not due until Thursday.

OpenStudy (raffle_snaffle):

I need to get some rest though. Thanks for your help and have a goodnight!

jimthompson5910 (jim_thompson5910):

you're welcome

OpenStudy (raffle_snaffle):

wrong code. I need to make a new thread

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!