sum up each column of my loop matlab

1 view (last 30 days)
mabdorab
mabdorab on 7 Dec 2016
Commented: Geoff Hayes on 7 Dec 2016
for i=[0:1:30000];
R=[poissrnd(0.0000407430680),poissrnd(0.000472626),poissrnd(0.002497495),poissrnd(0.996989136)];
end
How can I get matlab to sum up each value of the R vector for all 30,000 simulations.
once its found the values from 1:i:30,000 I want it to add all the values in the first column, then all values in the second column, etc and have the result in one vector
  2 Comments
mabdorab
mabdorab on 7 Dec 2016
sorry and just to add, if I wanted to find out where the values in columnSums came from how would I do this, so I know which iteration it came from. I am doing a forecast for nuber of claims and knowing where the counts came from may be required in my analysis.
Thanks in advance
Geoff Hayes
Geoff Hayes on 7 Dec 2016
columnSums is an array of the sums of each column of R....where R is an array of Poisson random numbers for 30000 iteration. Please clarify your question because it isn't clear what you are asking.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 7 Dec 2016
mabdorab - it sounds like you want to create a 30000x4 array of Poisson random numbers. Try presizing your R and then update each row as you iterate over k. For example,
n = 30000;
R = zeros(n,4);
for k=1:n
R(k,:) = [poissrnd(0.0000407430680),poissrnd(0.000472626),poissrnd(0.002497495),poissrnd(0.996989136)];
end
Then you can use sum to sum your columns as
columnSums = sum(R);
  2 Comments
mabdorab
mabdorab on 7 Dec 2016
perfect thank you so much.
What would I do if I wanted the columnSums first value to be called A, second value B, third value C, fourth value D, and had them result seperatley?
really appreciate the fast response, my project is due in 2 days.
Geoff Hayes
Geoff Hayes on 7 Dec 2016
Do you mean that you want to create a table? For example,
array2table(columnSums, 'VariableNames',{'A','B','C','D'});

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!