Help writing For loops
Show older comments
Hi,
I'm trying to shrink the length and do the following analysis faster. Could someone tell me how to create a for loop or any other suggestions that would for the following two examples:
Example 1: I have data with two different locations and in each location there are two different areas. Each area containing 11 sensors. I'm trying to create a linear regression for each sensor at each area and make a scatter plot.
%Siliwa A Horizon
%Probe P
RDP_P_Si_A = RAW_DATA((RAW_DATA(:,1)==11) & (RAW_DATA(:,2)==1),9);
%%Two different areas are denoted 11,12,21,22. Each sensor has a number assigned from 1-11 so in this case the 11 means site 1, area 2 and the 1 in the second portion is the probe number
Theta_P_Si_A = Theta((RAW_DATA(:,1)==11) & (RAW_DATA(:,2)==1),1);
[b_Probe_P_Si_A,dev_Probe_P_Si_A,st_Probe_P_Si_A] = LinearModel.fit(RDP_P_Si_A,Theta_P_Si_A);
[b_Probe_P_Si_A,st_Probe_P_Si_A.p];%coefficients and p-values
figure
scatter(RDP_P_Si_A,Theta_P_Si_A,'.')
xlim([0,70])
ylim([0,0.8])
xlabel('RDP, Probe P')
ylabel('Volumetric Water Content')
set(findobj('type','axes'),'fontsize',12)
set(gcf, 'PaperPositionMode','auto')
print('Siliwa_A_Probe_P','-dpng')
%Probe Q
%Probe Q
RDP_Q_Si_A = RAW_DATA((RAW_DATA(:,1)==11) & (RAW_DATA(:,2)==2),9);
Theta_Q_Si_A = Theta((RAW_DATA(:,1)==11) & (RAW_DATA(:,2)==2),1);
[b_Probe_Q_Si_A,dev_Probe_Q_Si_A,st_Probe_Q_Si_A] = LinearModel.fit(RDP_Q_Si_A,Theta_Q_Si_A);
[b_Probe_Q_Si_A,st_Probe_Q_Si_A.p];
figure
scatter(RDP_Q_Si_A,Theta_Q_Si_A)
xlim([0,70])
ylim([0,0.8])
xlabel('RDP, Probe Q')
ylabel('Volumetric Water Content')
set(findobj('type','axes'),'fontsize',12)
set(gcf, 'PaperPositionMode','auto')
print('Siliwa_A_Probe_Q','-dpng')
...and so for with the above code until I get to probe Z
2 Comments
Julie
on 29 Jan 2016
Give us a description of how the data are stored; it seems you're doing a lookup but the code is very difficult to read owing to such duplication of long names...I took the liberty to turn the (extremely long) comment into text so it would word wrap to try to be able to infer something from it but it wasn't really all that helpful...
Well, let's see if take a couple cases and try to distill them into their essence; maybe we can infer what's going on--
R_A=DATA(( DATA(:,1)==11)&(DATA(:,2)==1),9);
T_A=Theta((DATA(:,1)==11)&(DATA(:,2)==1),1);
b_A= LinearModel.fit(R_A,T_A);
%Probe Q
R_A=DATA(( DATA(:,1)==11) & (DATA(:,2)==2),9);
T_A=Theta((DATA(:,1)==11) & (DATA(:,2)==2),1);
b_A= LinearModel.fit(R_A,T_A);
...
Rearranging to compare
R_A=DATA(( DATA(:,1)==11)&(DATA(:,2)==1),9);
R_A=DATA(( DATA(:,1)==11)&(DATA(:,2)==2),9);
T_A=Theta((DATA(:,1)==11)&(DATA(:,2)==1),1);
T_A=Theta((DATA(:,1)==11)&(DATA(:,2)==2),1);
OK, this seems to be for a given index on column 1, iterate over the values in column 2. If you have a counted number of values in the second column and the list is complete then one can use the counted loop; otherwise one can use unique to get the actual ID numbers and iterate over that collection. From the description given, I'll presume the former
nRegion=11; % Guessing is region
ixRgn=DATA(:,1)==nRegion; % the fixed index
rData=DATA(ixRgn,9); % subset on region
tData=DATA(ixRgn,1); % R and Theta arrays
% no reason to keep doing the above every time
nSensors=11; % Fix up the number as needed
for i=1:nSensors
ix=rdata(:,2)==i: % select the sensor for loop
x=rdata(ix,9); % the x-data for regression
y=yData(ix,1); % and y-data for regression
b=LinearModel.fit(x,y); % do the regression
% all the plotting and so on goes here using
% the computed coefficients, etc.
...
end
Add back in the additional outputs wanted; I just saved the coefficient array to reduce the code to the essence so don't get lost in the forest for the noise of the trees so can see the overall scheme.
Also, if you need to keep the outputs for more than just the temporary results, then allocate an array of length(nSensors) by the needed columns and populate it as
b(i,:)=LinearModel.fit(x,y);
etc. Or, use a cell array to contain the outputs.
If there's also another iteration over the alternate index, then it's simple enough to do as another outer loop and then the first index will also be a variable of that loop index.
Answers (0)
Categories
Find more on Functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!