Find mean of rows containing decimal numbers in between integers in a column

2 visualizaciones (últimos 30 días)
I have a column with mostly decimal numbers and some integers
Example (the integers are in bold). - Y = [1 0.098 0.00076 0.01 2 0.099 0.007 2 0.003 0.04 0.1 4]. The integers range from 1 - 4.
I want to find the average of the numbers in between the integers, essentially [1 mean(0.098 0.00076 0.01) 2 mean (0.099 0.007) 2 mean(0.003 0.04 0.1) 4].
What would be the best way to do this? Is it recommended I turn the integers into a grouping variable?

Respuesta aceptada

madhan ravi
madhan ravi el 17 de Ag. de 2019
Editada: madhan ravi el 17 de Ag. de 2019
Y = [1 0.098 0.00076 0.01 2 0.099 0.007 2 0.003 0.04 0.1 4];
Y=Y(:);
ix=diff(find(~mod(Y,1)))-1;
assert(nnz(~mod(Y,1))>2,'atleast one gap between integers is required')
y=Y(mod(Y,1)~=0);
V=mat2cell(y(1:sum(ix)),ix);
Wanted=cellfun(@mean,V)
  7 comentarios
madhan ravi
madhan ravi el 17 de Ag. de 2019
Editada: madhan ravi el 17 de Ag. de 2019
Try the below, there was a minor error before:
Y=Y(:);
ii=~mod(Y,1);
ix=diff(find(ii))-1;
y=Y(mod(Y,1)~=0);
assert(~isempty(nonzeros(ix)),'Atleast one gap between integers is required')
V=mat2cell(y(1:sum(ix)),ix);
W=cellfun(@mean,V);
Wanted = zeros(nnz(ii)+numel(W),1);
Wanted(1:2:end) = Y(ii);
Wanted(2:2:end) = W
Aleya Marzuki
Aleya Marzuki el 17 de Ag. de 2019
Editada: Aleya Marzuki el 18 de Ag. de 2019
Cool, thanks again!
*EDIT*
I accepted this answer in the end because it accounts for there occasionally being no spaces between integers, which is what my actual data has (again should have specified this in my question, apologies).

Iniciar sesión para comentar.

Más respuestas (3)

Stephen23
Stephen23 el 17 de Ag. de 2019
Editada: Stephen23 el 18 de Ag. de 2019
>> Y = [1,0.098,0.00076,0.01,2,0.099,0.007,2,0.003,0.04,0.1,4];
>> X = cumsum([1;diff(~mod(Y(:),1))]~=0);
>> Z = accumarray(X(:),Y(:),[],@mean)
Z =
1
0.03625333333333333
2
0.05300000000000001
2
0.04766666666666667
4
EDIT: minor changes based on comments below.
  6 comentarios
Andrei Bobrov
Andrei Bobrov el 17 de Ag. de 2019
+1
Y = [1,0.098,0.00076,0.01,2,0.099,0.007,2,0.003,0.04,0.1,4];
out = accumarray(cumsum([1;diff(mod(Y(:),1) == 0)~=0]),Y(:),[],@mean)
Bruno Luong
Bruno Luong el 17 de Ag. de 2019
Editada: Bruno Luong el 17 de Ag. de 2019
Compact little gem, though I'm not fan of "~~x", IMO "x~=0" is clearer and perhaps faster.

Iniciar sesión para comentar.


darova
darova el 17 de Ag. de 2019
Use ismembertol() or ismember() (round values to some tolerance if needed) to find indices of integer values
Use loop to find mean

KALYAN ACHARJYA
KALYAN ACHARJYA el 17 de Ag. de 2019
Editada: KALYAN ACHARJYA el 17 de Ag. de 2019
Its just the Jugaar non-efficient code
# Recomended not to use, I tried few minutes, hence I posted here
Y=[1 0.098 0.00076 0.01 2 0.099 0.007 2 0.003 0.04 0.1];
data1=find(Y>=1);
mean_data=zeros(1,length(data1));
for i=1:length(data1)
if i==length(data1)
mean_data(i)=mean(Y(data1(i)+1:end));
else
mean_data(i)=mean(Y(data1(i)+1:data1(i+1)-1));
end
end
mean_data
Result:
mean_data =
0.0363 0.0530 0.0477
*Elapsed time is 0.013581 seconds.
  2 comentarios
madhan ravi
madhan ravi el 17 de Ag. de 2019
Editada: madhan ravi el 17 de Ag. de 2019
Kalyan the result is completely wrong. How did you assume it was 3??
mean_data=zeros(1,3);
How would you preallocate mean_data for the below example???
Y=[1 0.098 0.00076 0.01 2 0.099 0.007 2 0.003 0.04 0.1 4 .3 .1 .4 3 0 3];

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by