Hi!
I have a cell array named data (4856x24). The data looks as follows
I want to write a function that averages out the values in each row for positive emotions (Happy,Romantic,Devotional,Calm) and negative emotions(Angry,Longing,Sad,Tensed).The averaged out value column names should be positive and negative respectively. Can anyone pls help. Thanks in advance.

2 comentarios

Adam
Adam el 11 de Sept. de 2014
I'm not sure what you mean by averaging out when the inputs are strings.
I would suggest you use a struct or table (if you are using Matlab 2014) though for your data as having column headings as the first row of a cell array is a lot less easy to work with than having them as fields of a struct or table headings.
avantika
avantika el 12 de Sept. de 2014
Editada: avantika el 12 de Sept. de 2014
Hi!
I am using matlab 2013a.

Iniciar sesión para comentar.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 11 de Sept. de 2014

0 votos

t1 = ~cellfun('isempty',regexp(data(1,:),'Happy|Romantic|Devotional|Calm'));
t2 = ~cellfun('isempty',regexp(data(1,:),'Angry|Longing|Sad|Tensed'));
out = [data(1,:);cell(1,size(data,2))];
out(2,t1) = num2cell(mean(cell2mat(data(2:end,t1))));
out(2,t2) = num2cell(-mean(cell2mat(data(2:end,t2))));

3 comentarios

avantika
avantika el 12 de Sept. de 2014
Hi Andrew! This code does not give me what I was expecting. I would rephrase my question.
Each row contains ratings of a participant for 8 emotions - 4 positive emotions (Happy,Romantic,Devotional,Calm) and 4 negative emotions(Angry,Longing,Sad,Tensed). For each participant (i.e each row) I would like to average the ratings for positive and negative emotions. Then, these ratings should be put in two separate columns named positive and negative.
avantika
avantika el 12 de Sept. de 2014
Thanks a lot! I was able to this myself, by modifying your code a bit!
Andrei Bobrov
Andrei Bobrov el 12 de Sept. de 2014
Hi Avantika! I beg to fulfill the request by Sean, he will show us a more better solution with table array.

Iniciar sesión para comentar.

Más respuestas (1)

Sean de Wolski
Sean de Wolski el 11 de Sept. de 2014
Editada: Sean de Wolski el 11 de Sept. de 2014

1 voto

If you convert that to a table (cell2table) and covert the feelings to a categorical, you can then use grpstats and varfun to do this for you, grouping by whatever you want.
If you post the mat file (as a zip) I can help you through these steps, it'll be much simpler and easier to understand than regular expressions/cellfun.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Sept. de 2014

Comentada:

el 12 de Sept. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by