sum up each column of my loop matlab

조회 수: 2 (최근 30일)
mabdorab
mabdorab 2016년 12월 7일
댓글: Geoff Hayes 2016년 12월 7일
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
mabdorab
mabdorab 2016년 12월 7일
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 2016년 12월 7일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 12월 7일
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
mabdorab
mabdorab 2016년 12월 7일
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 2016년 12월 7일
Do you mean that you want to create a table? For example,
array2table(columnSums, 'VariableNames',{'A','B','C','D'});

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by