MATLAB Answers

# Cummulation in the rows

1 view (last 30 days)
Harsh Rob on 13 Aug 2019
Commented: Star Strider on 13 Aug 2019
I have 234 rows and 10,000 columns. I want to find out the value of the integral as mentioned in the screenshot. For this, I am using the Riemann integral. The procedure I am following is -
One column operation only
1. Find the standard deviation of 1 element only - I have initialised as 0.
2. Find the standard deviation of 1st and 2nd element only - Input in the matrix
3. Find the standard deviation of 1st, 2nd and 3rd element only - Input in the matrix
4. This is done in a loop for 234 times only.
5. Finally the sum of the entire column is taken to find the value of the intergral (using the Riemann intergral)
The same step needs to be done for 10,000 times and thus, the output will be a matrix of 1*10,000.
My code (only for 1 column) is as follows which does not work-
X1= X(:,1); %Gives the first column of the entire matrix of 10,000
n=length(X1) %Finds the length of the column to repeat the number of iterations
for i=1:n %For loop to run the same process from row 1 to row 234 in column 1 only
i %Gives the process number where it is running
sumpara(1) = X1(1); Initialised the first value of the sum
sumpara(i+1)= sumpara(i)+X1(i+1); %Gives the formula for the summing up the row elements in the same column
if(i>=n) %Was given an error- "Index exceeds the number of array elements (234)." So i put this code, but it keeps running the program and gives me "Continue entering statement" in the status bar.
break
else continue
end
sumparat=sumpara' %Wanted the output as a column, so taken the trasnspose
I have mentioned the comments for every line in the code. Can anyone help me in finding the error and correcting the code?
Thanks in advance.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 13 Aug 2019
Try this:
X = randi(9, 234, 1E+4); % Create Matrix
for k = 1:size(X,1)
sd(k,:) = std(X(1:k,:),[],1); % Standard Deviation Matrix
end
RiemannIntegral = sum(X); % Reimann Sum
##### 4 CommentsShowHide 3 older comments
Star Strider on 13 Aug 2019
As always, my pleasure.
I have no idea what you are doing in Excel, so I do not know what the difference could be due to. My code does what you said that you wanted to do.
I am aware that Excel does its calculations much differently than MATLAB, even though they may appear to be the same. (I have encountered this problem previously, in other Questions.)

Sign in to comment.

R2019a

### Community Treasure Hunt

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

Start Hunting!