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

1 Ansicht (letzte 30 Tage)
Blue
Blue am 24 Jun. 2024
Kommentiert: Star Strider am 24 Jun. 2024
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)

Akzeptierte Antwort

Star Strider
Star Strider am 24 Jun. 2024
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 Kommentare
Blue
Blue am 24 Jun. 2024
Thats pretty great ! Thank you !
Star Strider
Star Strider am 24 Jun. 2024
As always, my pleasure!
It took a bit of time and experimentation to devise the most optimal approach.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by