Perform processing over each row of table starting from first non-zero element in that row

2 visualizaciones (últimos 30 días)
Hello is there a way to perform mean and movmean of each row of the table starting from its first non zero element?

Respuestas (1)

Matt J
Matt J el 6 de Jul. de 2020
Your question implies that all elements of the table are numeric., e.g.,
T =
5×3 table
Var1 Var2 Var3
_______ _______ _______
0 0 0.66783
0.95769 0 0
0 0 0
0.67612 0.25479 0
0 0 0.67533
If so, you may as well pre-convert it to a numeric array because...what is the benefit of holding it as a table anyway if they are all numbers?
>> T=table2array(T)
T =
0 0 0.6678
0.9577 0 0
0 0 0
0.6761 0.2548 0
0 0 0.6753
At this point, I would convert all initial zeros to NaN:
[~,first]=max(logical(T));
nanmap=(1:size(T,1)).'<first;
T(nanmap)=nan;
>> T
T =
NaN NaN 0.6678
0.9577 NaN 0
0 NaN 0
0.6761 0.2548 0
0 0 0.6753
and now you can simply use any of the column-wise mean functions with nanflag='omitnan'. For example,
>> result=movmean(T,3,1,'omitnan');
>> result(nanmap)=0
result =
0 0 0.3339
0.4788 0 0.2226
0.5446 0 0
0.2254 0.1274 0.2251
0.3381 0.1274 0.3377
  1 comentario
Matt J
Matt J el 6 de Jul. de 2020
If you truly must have the result in table form, it is simple enough to convert it:
>> result=array2table(result,'VariableNames',{'Var1','Var2','Var3'})
result =
5×3 table
Var1 Var2 Var3
_______ _______ _______
0.95769 NaN 0.33392
0.47885 NaN 0.22261
0.54461 0.25479 0
0.22537 0.1274 0.22511
0.33806 0.1274 0.33767

Iniciar sesión para comentar.

Categorías

Más información sobre Tables 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