Creating a conditional variable or function such that

6 visualizaciones (últimos 30 días)
JB
JB el 3 de Jun. de 2020
Comentada: Ameer Hamza el 4 de Jun. de 2020
Hi, I'm struggling for hours to do this...
Suppose I have this data:
schoolid classid classsize enrollment newcolumn
1001 1 10 22 11
1001 2 12 22 11
1005 1 14 29 14.5
1005 2 15 29 14.5
1018 1 8 22 22/3
1018 2 4 22 22/3
1018 3 10 22 22/3
.
.
.
I would like to generate a function that gives "newColumn" above.
Plaese help me out. Thanks.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 3 de Jun. de 2020
Try this
schoolID = [1001 1001 1005 1005 1018 1018 1018];
enrollment = [22 22 29 29 22 22 22];
grps = findgroups(schoolID);
avg = splitapply(@(x) x(1)/numel(x), enrollment.', grps.');
newcolumn = avg(grps);
  2 comentarios
JB
JB el 3 de Jun. de 2020
Wow... Thank you so much! It worked perfectly. I don't know how it worked. I will study closely each codes. Anyways, thanks again!
Ameer Hamza
Ameer Hamza el 4 de Jun. de 2020
I am glad to be of help! Documentation pages will be helpful in understanding this.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by