Borrar filtros
Borrar filtros

How to do a cross-multiplication by group in a table

30 visualizaciones (últimos 30 días)
Blue
Blue el 24 de Jun. de 2024 a las 12:42
Comentada: Star Strider el 24 de Jun. de 2024 a las 16:33
Hi. I am wondering how to do a cross-multiplication (rule of three) by group in a table. More specifically I have an initial table in which I want to fill the NaNs by doing a cross-multiplication between c and d. For example the first NaN is (10*40) / 1 == 400. This need to be applied for unique combinations of a and b.
% Initial table
a = [1, 1, 1, 1, 1, 1, 2, 2]';
b = [45,45,12, 17, 56, 56, 45, 45]';
c = [1, 10, 3, 9, 2, 10, 1, 10]';
d = [40, NaN, 1, 9, 20, NaN, 40, NaN]';
t = table(a, b, c, d)
% Desired end result
e = [1, 1, 1, 1, 1, 1, 2, 2]';
f = [45,45,12, 17, 56, 56, 45, 45]';
g = [1, 10, 3, 9, 2, 10, 1, 10]';
h = [40, 400, 1, 9, 20, 100, 40, 400]';
result = table(e, f, g, h)

Respuesta aceptada

Star Strider
Star Strider el 24 de Jun. de 2024 a las 12:50
I am not certain what you want to do, however the fillmissing function can replace the NaN values in ‘d’ with the scalar 400.
Try this —
d = [40, NaN, 1, 9, 20, NaN, 40, NaN]';
h = fillmissing(d, 'Constant',400)
h = 8x1
40 400 1 9 20 400 40 400
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.
  8 comentarios
Blue
Blue el 24 de Jun. de 2024 a las 16:27
Thats pretty great ! Thank you !
Star Strider
Star Strider el 24 de Jun. de 2024 a las 16:33
As always, my pleasure!
It took a bit of time and experimentation to devise the most optimal approach.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by