0

I have 'M' users that have two different characteristics of N1 and N2 possible values for each case. I.E: A is a Mx1 vector that especifies the sex {'F','M'} (N1 = 2) of each user and B is a Mx1 vector that especifies the continent where the user lives {'ASIA','AFRICA','AMERICA',etc.} (N2 = 6). I want to create a N1xN2 (2*6) matrix that gives me the number of users for each combination of sex and continent without using a for loop. I've tried this code:

bsxfun(@eq,A,unique(A)')'*bsxfun(@eq,B,unique(B)')

But it is not what i need.

Thanks in advance.

PS: I do not need the cartesian product of the values, I need a matrix counting the elements for each combination.

Eric
  • 3
  • 3
  • I don't think it's a duplicate. Eric, you can do the following [~,~,ia] = unique(A);[~,~,ib] = unique(B);M = accumarray([ia, ib]); Not tried but should work – G.J May 05 '17 at 20:00
  • `M = accumarray([ia, ib])` throws he following error: "Error using accumarray Not enough input arguments." I've changed to `M = accumarray(ia, ib)` , but it returns a vector with the count for one characteristic only. – Eric May 09 '17 at 15:20
  • Sorry, thought the snd argument was optional. Try 'accumarray([ia,ib], 1)' – G.J May 09 '17 at 19:39
  • @rayryeng: Why do you think it's a duplicate ? – G.J May 09 '17 at 19:49
  • @G.J Upon seeing the edits, this is not a duplicate. I'll reopen. Please go ahead and answer the question. – rayryeng May 09 '17 at 20:47

1 Answers1

0

You'll need accumarray here :

[~,~,ia] = unique(A);
[~,~,ib] = unique(B);
M = accumarray([ia, ib], 1);
G.J
  • 795
  • 1
  • 6
  • 12