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

matlab

OpenStudy (ebayminer126):

? @raffle_snaffle

OpenStudy (raffle_snaffle):

@jim_thompson5910

jimthompson5910 (jim_thompson5910):

what's your question @raffle_snaffle ?

OpenStudy (raffle_snaffle):

how do you extract certain years out of a large matrix using the find command?

OpenStudy (raffle_snaffle):

jimthompson5910 (jim_thompson5910):

which page are you on?

jimthompson5910 (jim_thompson5910):

page 3?

OpenStudy (raffle_snaffle):

page 2 of 4 bottom of the page where I need to find a precip_2013 = find()

OpenStudy (raffle_snaffle):

wait hold on think i figured it out.

jimthompson5910 (jim_thompson5910):

ok post what you get

OpenStudy (raffle_snaffle):

precip_2013 = find((gcdata2 < 2014) & (gcdata2 < 2015) & (gcdata2 < 2016))

OpenStudy (raffle_snaffle):

okay maybe not

jimthompson5910 (jim_thompson5910):

you just want 2013, so why not say `indices = find(year == 2013)` recall that you stored all the year values in the `year` vector. The stored indices will house the addresses of the rows where the precipitation data corresponds to 2013

jimthompson5910 (jim_thompson5910):

btw `(gcdata2 < 2014) & (gcdata2 < 2015) & (gcdata2 < 2016)` is very inefficient because saying x < 2014 AND x < 2015 AND x < 2016 is the same as x < 2014 even then, it's better to just say x == 2013 to get that specific year you want

OpenStudy (raffle_snaffle):

I forgot about the year vector.

jimthompson5910 (jim_thompson5910):

you have to access just the year vector or else you may pick up on data that has 2013 in it that isn't a year value. But that seems unlikely. Even then, it's a good idea to pull the data apart like this

OpenStudy (raffle_snaffle):

% finds data for precipitation precip_2013 = find(year == 2013); year(precip_2013);

jimthompson5910 (jim_thompson5910):

so that will just spit out a bunch of 2013's

OpenStudy (raffle_snaffle):

Yes, I would agree. But is there a way to extract just 2013 from the gcdata2 = [year', month', day', gcdata1]

jimthompson5910 (jim_thompson5910):

can i see your m file?

jimthompson5910 (jim_thompson5910):

i forget how gcdata1 is set up

OpenStudy (raffle_snaffle):

clc; clear % clears command and workspace window load('gcdata'); % loads gcdata format shortg; % formats gcdata % imports excel file into Matlab gcdata = xlsread('govt_camp.xlsx'); % new matrix B, eliminates last two columns Ngcdata = gcdata(:, [1:end - 2]); % converting first column into a string [m, n] = size(Ngcdata); for k = 1:1:m DateString = sprintf('%d',Ngcdata(k,1)); % converts the date value from a number to string year(k) = str2double(DateString(1:4)); % reads off the first 4 digits of DateString and stores in year matrix month(k) = str2double(DateString(5:6)); % reads off next two digits day(k) = str2double(DateString(7:8)); % reads off next one digit end gcdata1 = Ngcdata(:, [2:end]); % removes first colum in Ngcdata gcdata2 = [year', month', day', gcdata1]; % new matrix gcdata2 % finds data for precipitation precip_2013 = find(year == 2013); year(precip_2013);

jimthompson5910 (jim_thompson5910):

ok right, first column cut off

jimthompson5910 (jim_thompson5910):

which column holds the precip data?

OpenStudy (raffle_snaffle):

jimthompson5910 (jim_thompson5910):

it's column 4 column1 = year column2 = month column3 = day column4 = "Precipitation (inches)" column5 = "Snow Depth (inches)" column6 = "Snowfall (inches)"

jimthompson5910 (jim_thompson5910):

so you'll look through just `gcdata2(:,4)` basically every row and column4

jimthompson5910 (jim_thompson5910):

here's how I'd do it % finds indices (aka address) for precip data in 2013 precip_indices_2013 = find(year == 2013); % store just the precipitation values in a temporary matrix temp = gcdata2(:,4) % pull out the precipitation values that correspond to year 2013 % uses the indices found previously precip_2013 = temp(precip_indices_2013) that should give you a vector `precip_2013` of precipitation values for the year 2013

jimthompson5910 (jim_thompson5910):

I guess you could rename `temp` to `precipitation_values` to be more specific

OpenStudy (raffle_snaffle):

okay well my precip_2013 vector doesn't match excels

jimthompson5910 (jim_thompson5910):

what do you mean?

OpenStudy (raffle_snaffle):

% finds data for precipitation precip_indices_2013 = find(year == 2013); % locates the indices for years year(precip_indices_2013); % extracts 2013 years from year vector temp_v = gcdata(:,4) % temporar vector for precip measured in inches precip_2013 = temp_v(precip_indices_2013); % actual precip_2013 values precip_2013 = 0 0 0 0 0 0 1 0.1 0 6 2 0 0 0 0 0

OpenStudy (raffle_snaffle):

Those are the first couple values of the output, but look at the excel file.

jimthompson5910 (jim_thompson5910):

you're accessing the wrong gcdata matrix

OpenStudy (raffle_snaffle):

my bad lol

jimthompson5910 (jim_thompson5910):

somehow you ended up in the wrong column. You accessed column 6 instead of column 4

OpenStudy (raffle_snaffle):

Okay it's fixed

OpenStudy (raffle_snaffle):

okay I should be able to do the next following steps. It wants me to find the precip for 2014, 2015, and 2016 as well.

jimthompson5910 (jim_thompson5910):

btw you don't need `year(precip_indices_2013);` in your code

OpenStudy (raffle_snaffle):

yeah I guess you are right. I was using it to check something though.

jimthompson5910 (jim_thompson5910):

well you suppressed the output so it doesn't do anything. I guess beforehand you didn't have the semicolon at the end

jimthompson5910 (jim_thompson5910):

I see what you mean though

OpenStudy (raffle_snaffle):

I will leave it and suppress the output.

OpenStudy (raffle_snaffle):

we are using a bar graph right? My bar graph looks similar to the image displayed on the lab. Also I am working on getting a title, x label, and y label on the graph too.

jimthompson5910 (jim_thompson5910):

in your pdf, it looks like a line graph

OpenStudy (raffle_snaffle):

nvm I got the subplot to work correctly with the correct title, x label, and y label

OpenStudy (raffle_snaffle):

% finds 2013 precip data precip_indices_2013 = find(year == 2013); % locates the indices for years year(precip_indices_2013); % extracts 2013 years from year vector temp_13 = gcdata2(:, 4); % temporar vector for precip measured in inches precip_2013 = temp_13(precip_indices_2013); % actual precip_2013 values from col. 4 bar2013 = bar(precip_2013); subplot(2, 2, 1); title('2013 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)'); % find 2014 precip data precip_indices_2014 = find(year == 2014); year(precip_indices_2014); temp_14 = gcdata2(:, 4); precip_2014 = temp_14(precip_indices_2014); bar2014 = bar(precip_2014); subplot(2, 2, 2); title('2014 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)');

OpenStudy (raffle_snaffle):

my subplots are reversed and the output is weird looking. Does the code look correct or do I have something backwards? This is for 2013 and 2014.

jimthompson5910 (jim_thompson5910):

why are you doing a bar graph?

jimthompson5910 (jim_thompson5910):

I see a line graph on page 2

OpenStudy (raffle_snaffle):

i am reading the lab. I don't see where it says "use a line graph".

OpenStudy (raffle_snaffle):

well if I am using a plot(x, y) command I know why my subplots won't work correctly.

jimthompson5910 (jim_thompson5910):

I'm looking at the examples given

jimthompson5910 (jim_thompson5910):

top of page 2 `Matlab Figure 1: You will create a Matlab Figure 1 that will end up looking like this document’s Figure 1.` then it shows a bunch of line graphs

OpenStudy (raffle_snaffle):

okay, thanks. I am trying to get the subplots to work.

OpenStudy (raffle_snaffle):

subplot need to be before or after plot(x,y)?

jimthompson5910 (jim_thompson5910):

before plot

jimthompson5910 (jim_thompson5910):

subplot sets up the empty slots for the plots to go in

OpenStudy (raffle_snaffle):

% finds 2013 precip data precip_indices_2013 = find(year == 2013); % locates the indices for years year(precip_indices_2013); % extracts 2013 years from year vector temp_13 = gcdata2(:, 4); % temporar vector for precip measured in inches precip_2013 = temp_13(precip_indices_2013); % actual precip_2013 values from col. 4 subplot(2, 2, 1); plot(year, temp_13); title('2013 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)'); % find 2014 precip data precip_indices_2014 = find(year == 2014); year(precip_indices_2014); temp_14 = gcdata2(:, 4); precip_2014 = temp_14(precip_indices_2014); subplot(2, 2, 2); plot(year, temp_14); title('2014 precipitation (in)'); xlabel('years (t)'); ylabel('snowfall (in)');

OpenStudy (raffle_snaffle):

How does this look? It seems to run fine.

jimthompson5910 (jim_thompson5910):

you didn't set up the 2013 plot correctly

jimthompson5910 (jim_thompson5910):

you created precip_2013 just fine, but you forgot to use it in the plot

jimthompson5910 (jim_thompson5910):

same for 2014

OpenStudy (raffle_snaffle):

I used temp_13 and temp_14 as my y in plot(x,y) because we are plotting snowfall with respect to years I thought.

OpenStudy (raffle_snaffle):

temp_13 and temp_14 is my data for precip measured in inches.

jimthompson5910 (jim_thompson5910):

here's how I'd do the 2013 plot % finds indices (aka address) for precip data in 2013 precip_indices_2013 = find(year == 2013); % store just the precipitation values in a temporary matrix temp = gcdata2(:,4); % pull out the precipitation values that correspond to year 2013 % uses the indices found previously precip_2013 = temp(precip_indices_2013); % pulls out the corresponding day values for the year 2013 days_2013 = day(precip_indices_2013); % set up plot subplot(2, 2, 1); plot(days_2013, precip_2013); title('2013 Daily Precipitation (in)'); xlabel('day of year'); ylabel('inches');

OpenStudy (raffle_snaffle):

why can't I use my code? lol

jimthompson5910 (jim_thompson5910):

you can. I'm just showing you how I did it. The way you set it up, you put the year along the x axis when it should be the day value. Also when you wrote `plot(year, temp_13);` you are using ALL the precip values when you want the 2013 precip values only

jimthompson5910 (jim_thompson5910):

so you're mixing up variables

OpenStudy (raffle_snaffle):

I am thinking.

jimthompson5910 (jim_thompson5910):

take all the time you need

OpenStudy (raffle_snaffle):

day isn't a vector? Don't you mean years

OpenStudy (raffle_snaffle):

I see the lab says days

jimthompson5910 (jim_thompson5910):

remember how we broke up the first column into 3 columns? years, months, days each column is a vector

jimthompson5910 (jim_thompson5910):

hmm it's either `day` or `days`. I forget what we named it

OpenStudy (raffle_snaffle):

ugh i see i see lol

jimthompson5910 (jim_thompson5910):

hmm now that I think about it, the days vector won't work. Because after 31, the days shouldn't wrap back to 1. It should keep counting up

OpenStudy (raffle_snaffle):

okay good because my graph looks really weird.

jimthompson5910 (jim_thompson5910):

hmm let me think

OpenStudy (raffle_snaffle):

I am just formatting my code

jimthompson5910 (jim_thompson5910):

we have to create a vector starting from 1 and going through to 365 to represent the 365 days of the year. So this vector will be a 365x1 matrix

OpenStudy (raffle_snaffle):

ok

jimthompson5910 (jim_thompson5910):

this might do the trick for k=1:1:365 day_of_year(k) = k; end though for 2016 it won't because 2016 isn't over yet. The graph is like 100 days into 2016

OpenStudy (raffle_snaffle):

I see what you are doing there. Why couldn't I use k = 1:1:365;

jimthompson5910 (jim_thompson5910):

you mean for the `for` loop?

OpenStudy (raffle_snaffle):

day_of_year is just a vector right?

jimthompson5910 (jim_thompson5910):

yes spanning from 1 to 365 (incrementing by 1)

OpenStudy (raffle_snaffle):

why do we need to use a for loop though?

jimthompson5910 (jim_thompson5910):

oh you mean day_of_year = 1:1:365; ??

OpenStudy (raffle_snaffle):

yeahhh

jimthompson5910 (jim_thompson5910):

hmm that might actually work, let me test

jimthompson5910 (jim_thompson5910):

yes it works, it makes a 1x365 matrix

jimthompson5910 (jim_thompson5910):

so you may have to transpose that

OpenStudy (raffle_snaffle):

okay, sorry I was thinking.

jimthompson5910 (jim_thompson5910):

day_of_year = [1:1:365]' will transpose it if you need it to be a column vector

OpenStudy (raffle_snaffle):

what is our y in plot(x,y) ?

jimthompson5910 (jim_thompson5910):

the precip data still

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!