Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Blue
Blue le 24 Juin 2024
Commenté : Star Strider le 24 Juin 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)

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by