1

This is continuation of my previous questions (one, two) and this is my real problem I have an array of data:

a   b   c   d
0   1   2   5
0   1   7   1
0   0   5   5
0   0   0   1
0   1   0   2

as you can see i want abc to be combine and in one column, just like this:

abc    d
012    5
017    1
005    5
000    1
010    2 

so i will now have 2 columns abc and d

next i want to count repeated values of d, add it up and combined those corresponding abc, see below

abc        d
012,005    10
017,010    2
010        5

so as you can see, 012, and 005 combine because they have the same value of d, and their d add up so it become 10, so how i can do that? this is my real problem please help thanks.

Community
  • 1
  • 1
Raldenors
  • 311
  • 1
  • 4
  • 15
  • Please be specific with your data structures! Your last question was not perfectly clear. Are `a`,`b`,`c`,`d` variable names or are they entries in the cell-array? Are your numbers given as numerics, strings, cell arrays of numerics or char arrays? – knedlsepp Feb 25 '15 at 13:46
  • 1
    You will draw more attention to this question if it came up with matlab code. i.e. ``a = [0; 0; 0; 0; 0]; b = [1; 1; 0; 0; 1];`` ... Also: are your vector elements strings or numbers? What datatype should ``abc`` be? a vector of cell-arrays? – Nras Feb 25 '15 at 13:47
  • they're number cell ,sir – Raldenors Feb 25 '15 at 13:52
  • a,b,c, and,d are just representation of columns, no need to include those letters – Raldenors Feb 25 '15 at 13:53
  • @Raldenors: Please look into the difference [*numeric array*](http://www.mathworks.com/help/matlab/matlab_prog/create-numeric-arrays.html) vs [*cell array containing numerics*](http://www.mathworks.com/help/matlab/cell-arrays.html) in Matlab. Those are quite different! People will have problems answering your questions if you confuse those! – knedlsepp Feb 25 '15 at 15:37

1 Answers1

4
A = [...
0   1   2   5;
0   1   7   1;
0   0   5   5;
0   0   0   1;
0   1   0   2 ]

%// column for identification
subs = A(:,4);
%// get order for sorting output
order = unique(subs,'stable')

%// get strings from numbers
abc = cellstr(reshape(sprintf('%i',A(:,1:3)),size(A(:,1:3))))

%// gather abc
groups = accumarray(subs,1:numel(subs),[],@(x) {abc(x)})

%// sum counts
counts = accumarray(subs,subs,[],@sum)

%// output
out = [groups num2cell(counts)]

%// reorder output
out = out(order,:)

%// filter output
out(find(~counts(order)),:) = []

out = 

    {2x1 cell}    [10]
    {2x1 cell}    [ 2]
    {1x1 cell}    [ 2]

with for example

out{1,1} = 

'005'
'012'

As you see out{1,1} is still not in the correct order. The reason is that accumarray is not stable. To solve it, use the function accumarrayStable provided in the link:

function A = accumarrayStable(subs, val, varargin)
[subs(:,end:-1:1), I] = sortrows(subs(:,end:-1:1));
A = accumarray(subs, val(I), varargin{:});
end

and you will finally get:

out{1,1} =

    '012'
    '005'
Community
  • 1
  • 1
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • ahm it appears that the output become 5x2 instead of 3x2, is there away to delete those rows with no values? thanks – Raldenors Feb 25 '15 at 13:57
  • and also the order was reverse, the combination 017,005, was located at the bottom instead at the top. – Raldenors Feb 25 '15 at 13:59
  • what could be the possible reason why i get an error: Error using accumarray First input SUBS must contain positive integer subscripts. – Raldenors Feb 25 '15 at 14:12
  • @Raldenors it depends on your input array. Is it exactly how you posted it? does it just contain positive integers? does it contain also zeros in the 4th column? – Robert Seifert Feb 25 '15 at 14:14
  • my data array is just like what i posted, it contains only positive, and there is no zero in 4th column just like what i posted – Raldenors Feb 25 '15 at 14:17
  • @Raldenors What happens if you copy&paste just my code. Does it work? `clear` your workspace before – Robert Seifert Feb 25 '15 at 14:19
  • yea it works, where i can place the: function A = accumarrayStable(subs, val, varargin) [subs(:,end:-1:1), I] = sortrows(subs(:,end:-1:1)); A = accumarray(subs, val(I), varargin{:}); end – Raldenors Feb 25 '15 at 14:23
  • @Raldenors either an extra function file or, in case your script is also a function, under your main function. – Robert Seifert Feb 25 '15 at 14:26
  • is there another way to display "012,005"? in the column? because it appears that it is on another column array, because i want to print that data – Raldenors Feb 25 '15 at 14:45
  • @Raldenors this is again another question. Include a minimal example there and be clear what you mean with "print". But not here in the comments, it's not a forum or chat. – Robert Seifert Feb 25 '15 at 14:47
  • i mean print is to be printed in printer. whenever i copy the the data and paste into word there is no "012,005" appear, and also check my desired output – Raldenors Feb 25 '15 at 14:50
  • @Raldenors, well seriously, I'm not a code writing service. Ask a new question. You never mentioned before that you want to print it out with a printer. This is a completely different issue. And your desired output is not possible, so I assumed it to be schematic. Be more clear please the next time. But in any way it would be a new question. – Robert Seifert Feb 25 '15 at 15:08
  • `accumarray`! `accumarray`! `accumarray`! – knedlsepp Feb 25 '15 at 15:33