MATLAB: Averaging in a cell array

average cell array

Hi!
I have a cell array named data (4856×24). 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.

Best Answer

  • 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))));