Adding rows to a column - average

Can anybody help me in the following:
I want to increase a column length by adding a an average value after each five rows. (i.e, if I have 10 rows, the output should be 12 rows, the 6th one will be the average of the first 5 rows, and the 12th one will be the average of the second 5 rows.)
Any help will be appreciated.
Thank you.

Let's Try this One
import statistics
x=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
final_list=[]
j=0
for i in range(0,len(x),5):
mean=statistics.mean(x[i:i+5])
final_list=final_list+x[i:i+5]
final_list.insert(j+i+5,mean)
j=j+1
print(final_list)

Related

rounding specific columns in matlab

I have 3 columns of data, but I need to round only the first and the last column and the second let as it is. So my "dream" format - round column, not round column, round column.
Could someone help me please?
.
A(:,1)=round(A(:,1));
% repeat above line for each column you want.
If you have many columns, you may want to store them in an index and loop
to_round=[1,3];
for ii in length(to_round)
A(:,to_round(ii))=round(A(:,to_round(ii)));
end

Tableau: Calculate the average value for the last 3 records

I have a time series data fro the different products. i like to calculate the average for each products based on only last 3 values. can you please help me.
Thanks
Kathir
Use SIZE() - INDEX() + 1
Drag it to your row or column and convert as discrete. Use table calc if needed
Now you will have an index for each row. Create another calculated field which will be using fix LOD of the above calculated field and when Index = 1 or 2 or 3, it sums it
You will be able to calculate average by diving the field with the sum of fix LOD created

how to calculate avrage of 1st 5 rows then next 5 row likewise continue in matlab

I have .xlsx file n rows and m column. I want to calculate average of 1st 5 rows then next 5 rows likewise ( each 5 interval average) continue for my data.
If A is your matrix, what about:
m=[];
for ii=1:5:20
m(end+1)=mean(mean(A(ii:ii+4,:)));
end
does it work for you?
Here is a "hack" that should be pretty fast if A is big:
m = mean(kron(speye(size(A,1)/5),ones(1,5))*A/5,2);
You can use reshape to get blocks of 5 rows in the first dimension. If A is your matrix
m = squeeze(mean(reshape(A,5,[],size(A,2)),1));
The code works as follows
reshape the matrix to get the blocks of 5 rows in the first dimension
Compute the mean over the blocks of 5 rows.
After the mean, the first dimension is a singleton so it is better to squeeze it so the output is a 2D matrix.

Summing a Column values Starting with the Last one in Matlab

is there a function in matlab that sums up values starting from the last row and susbstitue the next row with summed values? for example:
data= 1 result 21
2 20
3 18
4 15
5 11
6 6
GameOfThrows is on the right track, but you need an additional flipud when you're done:
out = flipud(cumsum(flipud(data)));
The first flip ensures that we start summing from the last element instead of the first. We then perform our cumulative sum but you also want to be sure that the order is reversed so you have to call flipud one more time. However, to be absolutely safe, because we don't know if your data is a row or column vector, I'm going to ensure that your data is a column vector before doing what you ask:
out = flipud(cumsum(flipud(data(:))));

Summing in groups of rows

I need help making sums in matlab, I have a column with this aspect;
7
2
1
0
5
2
8
7
(...)
And now I want to sum those numbers in groups of 4 rows and get a new matrix with those numbers, for this example I will get a new column with:
10 (7+2+1+0)
22 (5+2+8+7)
(...)
Thx for helping
Using reshape allows you to make a 4xn-matrix out of your data. Doing so, you can use sum.
sum(reshape(x,4,numel(x)/4),1)'