MATLAB Answers

Cummulation in the rows

2 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.

  0 Comments

Sign in to comment.

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

Show 1 older comment
Star Strider
Star Strider on 13 Aug 2019
I will do my best.
It uses the std function (that calculates the standard deviation) on the columns from row 1 through row k without weights (the [] is the empty matrix indicating that the function should use the default weight vector of ones) along the first (column) dimension (so all 10,000 columns), so that it takes the standard deviation of each of the columns. In MATLAB, dimension 1 is across rows (down columns), dimension 2 is down rows (across columns), and for multi-dimensional matrices (not applicable here), the dimension argument is for the appropriate dimension.
So, for example:
A = [1 2 3
4 5 6]
A1 = std(A,[],1)
A2 = std(A,[],2)
produces:
A =
1 2 3
4 5 6
A1 =
2.1213 2.1213 2.1213
A2 =
1
1
The other MATLAB functions that allow the dimension to be specified as an argument work the same way.
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)

Products


Release

R2019a