Cummulation in the rows

8 views (last 30 days)
Harsh Rob
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.

Accepted Answer

Star Strider
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 Comments
Harsh Rob
Harsh Rob on 13 Aug 2019
Thanks for that quick response and superb explanation. When I run this code for my dataset, it gives wrong result. I checked this using excel and comparing my results. The difference between the two results are very different. What could be the reason for this?
Star Strider
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.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!