44 views (last 30 days)

Hello,

I want to create a for loop that will sum specific columns of a single row of a matrix in a dataset. I then want to repeat that action for other datasets. The matrix is 37 rows x 100 columns. All datasets are from one mat. file.

% For Nubian Aquifer - 1900-1999

sum_ET = sum(aq_gwdctr_ET(1,:))/100;

sum_P = sum(aq_gwdctr_P(1,:))/100;

sum_R = sum(aq_gwd_qover(1,:))/100;

sum_dSdt = sum(aq_gwdctr_dSdt(1,:))/100;

sum_C = sum(aq_gwdctr_C(1,:))/100;

sum_GWpumping = sum(aq_irri4d(1,:))/100;

% For Nubian Aquifer - 1950-1999

sum_ET1 = sum(aq_gwdctr_ET(1,51:end))/50;

sum_P1 = sum(aq_gwdctr_P(1,51:end))/50;

sum_R1 = sum(aq_gwd_qover(1,51:end))/50;

sum_dSdt1 = sum(aq_gwdctr_dSdt(1,51:end))/50;

sum_C1 = sum(aq_gwdctr_C(1,51:end))/50;

sum_GWpumping1 = sum(aq_irri4d(1,51:end))/50;

% For Nubian Aquifer - 1900-1949

sum_ET2 = sum(aq_gwdctr_ET(1,1:50))/50;

sum_P2 = sum(aq_gwdctr_P(1,1:50))/50;

sum_R2 = sum(aq_gwd_qover(1,1:50))/50;

sum_dSdt2 = sum(aq_gwdctr_dSdt(1,1:50))/50;

sum_C2 = sum(aq_gwdctr_C(1,1:50))/50;

sum_GWpumping2 = sum(aq_irri4d(1,1:50))/50;

Currently, the code that I have performs all these individually. What I'm hoping to achieve is to simplify the above code. The new code should be able to;

A) For 1900-1999, sum all the columns for every row for each matrix, dataset. For 1950-1999, sum columns 51 until end for every row for each matrix, dataset. For 1900-1949, sum columns 1 until 50 for every row for each matrix, dataset.

B) This action should be repeated for each dataset (aq_gwdctr_ET, aq_gwdctr_P, etc.).

C) It would be a bonus if I could tabulate all these values.

% For year 1900-1999

Aquifer = [1; 2; 3]; % This is the rows in the matrix

ET = [sum_ET]; % Sum ET for each row in the matrix, repeat for each dataset

T = table(Aquifer, ET);

Thank you for your help.

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 3 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/495609-for-loop-through-multiple-datasets#comment_776027

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/495609-for-loop-through-multiple-datasets#comment_776027

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/495609-for-loop-through-multiple-datasets#comment_776376

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/495609-for-loop-through-multiple-datasets#comment_776376

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/495609-for-loop-through-multiple-datasets#comment_776378

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/495609-for-loop-through-multiple-datasets#comment_776378

Sign in to comment.