How can one reshape this table?

6 vues (au cours des 30 derniers jours)
alpedhuez
alpedhuez le 30 Juil 2021
Modifié(e) : Matt J le 30 Juil 2021
Consider a table T with
group date visitor
age 18-24 1/1/2021 10
age 18-24 1/2/2021 20
age 25-34 1/1/2021 10
age 25-34 1/2/2021 30
I want to reshape the matrix as
date age18-24 age 25-34
1/1/2021 10 10
1/2/2021 20 30
How can one do this?

Réponse acceptée

Matt J
Matt J le 30 Juil 2021
Modifié(e) : Matt J le 30 Juil 2021
Perhaps as follows?
load tst
T
T = 4×3 table
group date visitor _____________ ____________ _______ {'age 18-24'} {'1/1/2021'} 10 {'age 18-24'} {'1/2/2021'} 20 {'age 25-34'} {'1/1/2021'} 10 {'age 25-34'} {'1/2/2021'} 30
[Gage,IDage]=findgroups(T(:,1));
[Gdate,IDdate]=findgroups(T(:,2));
Tnew=[ IDdate, array2table( accumarray([Gdate,Gage],T{:,3}) , 'Var',IDage{:,1})]
Tnew = 2×3 table
date age 18-24 age 25-34 ____________ _________ _________ {'1/1/2021'} 10 10 {'1/2/2021'} 20 30

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by