Asked by Clifford Shelton
on 3 May 2012

With help I was able to create a non-scalar structure in order to automate the multiple calculations for a regression of sinwaves with a data set of runs scored of my favorite baseball team over 501 days with a sample of 1 day. See example code:

%My inputs y = Score(:); n = 501; sincos = rand(151,1) %what I need to mulitply the sin/cos with from identified cycles in a spectral analysis done prior% games = 1:501;

%pre-allocate structure data(1:151) = struct('X',NaN(501,3),'bhat',NaN(3,1),'yhat',NaN); %loop for ii = 1:151 tmp = 2*pi*(sincos(ii))*n; data(ii).X = rand(501,3); data(ii).X(:,2) = cos(tmp)'; data(ii).X(:,3) = sin(tmp)'; data(ii).bhat = data(ii).X\y; data(ii).yhat = data(ii).bhat(1)+data(ii).bhat(2)*cos(tmp)+data(ii).bhat(3)*sin(tmp); end

My questions is now how do I plot the newly calculated sinwaves that now fit the data and automate that process for all 151 sinwaves?

When I was doing the calculations manually I was entering:

plot (n,y,'b'); hold on plot (games,yhat(1),'r'); plot (games,yhat(2),'g'); plot (games,yaht(3),'y'); etc...etc...

I'm not clear how to automate this plotting process as well as pull the data I need from the newly created data structure. I'd hate to have to type the same line over and over 151 times!

*No products are associated with this question.*

Answer by Clifford Shelton
on 3 May 2012

Accepted answer

Ok...i think i figured out how to plot them. It doesn't need to be automated...I was able to pull all the data from the structure by just changing the original input code slightly:

y = Score(:); n = 501; t = (1:501)'; games = 1:501;

data(1:151) = struct('X',NaN(501,3),'bhat',NaN(3,1),'yhat',NaN);

for ii = 1:151 tmp = 2*pi*(sincos(ii))*t; data(ii).X = rand(501,3); data(ii).X(:,2) = cos(tmp)'; data(ii).X(:,3) = sin(tmp)'; data(ii).bhat = data(ii).X\y; data(ii).yhat = data(ii).bhat(1)+data(ii).bhat(2)*cos(tmp)+data(ii).bhat(3)*sin(tmp); end yhat = [data.yhat]; plot(games,yhat); hold on plot (games,Score);

Ok...I'm starting to get the hang of this Matlab thing I suppose!

Opportunities for recent engineering grads.

## 0 Comments