0

I have combined the folowing posts how to count unique elements of a cell in matlab?

Finding which letter has maximal occurence

to be able to find max occurance with penalty. i.e. cell = 'a' 'b' 'a' 'c' 'a' 'a' 'e' penalty= [5] [2] [4] [2] [5] [1] [0]

 letterWeight= [1*5] [1*2] .....

now letter count will be 'a'[5+4+5+1]
                         'b'[2]
                         'c'[2]
                         'e'[0]
now maxcount= as done in max occurance

i just need a hint maybe i am missing something you can ease things on me thanks

need to add something on the last line but how?

enter code here str= num2cell(Allquants{p});
                matchcell ={'a','b','c','d','e'};
                [~,index] =ismember(str,matchcell);
                count = accumarray(index(:),1,[numel(matchcell)  1]);

Now the code is:

      plaincount = accumarray(index(:),1,[numel(matchcell)  1]);
      count = accumarray(index(:),penalties{p}{r},[numel(matchcell)  1],@sum); 

Maybe it should be outside the loop? yes {r} should be removed

Community
  • 1
  • 1
pac
  • 291
  • 9
  • 19

1 Answers1

1

You need to put the penalties into the second argument of accumarray, since it is these values that will get summed up (note that penalty needs to be numeric, so you may have to call cell2mat):

count = accumarray(index(:),penalties,[numel(matchcell)  1],@sum);
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • this is Penalties for i=1: TotalnoOfGrids for j=1: noOfNodes penalty{j}=(round(digit_size/2))- (char(Allquants{i}(j))-char(mostCommonLetters {i})); end penalties{i}=penalty; end When I do cell2mat(Penalties{p}) it is doing error! thanks – pac Feb 29 '12 at 14:54
  • i was trying this [~,index] =ismember(str,matchcell)* Penalties{p}; without changing count – pac Feb 29 '12 at 14:57
  • ok done with: count = accumarray(index(:),penalties{p},[numel(matchcell) 1],@sum); thanks – pac Feb 29 '12 at 15:12
  • Hi @Jonas plaincount = accumarray(index(:),1,[numel(matchcell) 1]); count = accumarray(index(:),penalties{p}{r},[numel(matchcell) 1],@sum); are giving same results value count should be 7.3 giving 8 as if not counting penalties why? – pac Apr 10 '12 at 08:54