Filter löschen
Filter löschen

Classification of a matrix to 0 and 1 matrix

1 Ansicht (letzte 30 Tage)
Moe
Moe am 10 Jul. 2014
Kommentiert: Roger Stafford am 12 Jul. 2014
Hello everyone,
I want matrix A to be like matrix B
ID, age and sex groups are repeated in matrix A. Matrix B is classified age based on the sex group with counting the value from matrix A. if any value in any group was repeated more than 1, then total will appear in matrix B. For example, in matrix A: ID=5, Age group=2, Sex group=2--->Then in matrix B: the value (4,5) is equal by 2

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 11 Jul. 2014
Assuming A and B are numerical arrays arranged as shown in your diagram,
B = accumarray([2*A(:,2)+A(:,3)-2,A(:,1)],1,[2*max(A(:,2)),max(A(:,1))]);
  2 Kommentare
Moe
Moe am 11 Jul. 2014
Thanks Roger. I'm wondering if we can have matrix B same as follow picture:
Roger Stafford
Roger Stafford am 12 Jul. 2014
For the second version, just interchange the first and second columns in the formula:
B = accumarray([2*A(:,1)+A(:,3)-2,A(:,2)],1,[2*max(A(:,1)),max(A(:,2))]);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jos (10584)
Jos (10584) am 11 Jul. 2014
A = [1,7,1 ; 2,2,1 ; 2,4,2 ; 3,13,2 ; 3,11,2 ; 4,6,2 ; 5,2,2 ; 5,2,2 ; 5,9,1 ; 6,7,1 ; 6,10,2 ; 7,8,1 ; 7,6,2 ; 7,6,2 ; 7,6,1 ; 7,1,1 ; 7,12,2];
% If A is as above:
nID = 7 ;
nAge = 13 ;
nSex = 2 ;
B = reshape(accumarray(A(:,[3 2 1]),1,[nSex nAge nID]) ,[],nID)
  4 Kommentare
Joseph Cheng
Joseph Cheng am 11 Jul. 2014
hmmm? there seems to be a missing comment before mine where the person posting asked for it the other way around. so I commented how to rewrite your function. Jos you had it correct the first time corresponding to the question asked.
Moe
Moe am 12 Jul. 2014
Thanks Jos & Joseph Cheng.

Melden Sie sich an, um zu kommentieren.


Joseph Cheng
Joseph Cheng am 10 Jul. 2014
Bearbeitet: Joseph Cheng am 10 Jul. 2014
I would first create a matrix Btemp of size max(AgeGroup) by max(ID) by max(SexGroup) full of zeros. then do a loop for each row of A to add Btemp(AgeGroup,ID,SexGroup) with 1; after you loop for each row of A then make your B matrix by stagering both sexgroup
Air coding so pardon any syntax mistakes:
Btemp = zeros(max(A(:,2)),max(A(:,1)),max(A(:,3))); %create all zeros
%add 1 for each instance listed in matrix A.
for row=1:size(A,1)
Btemp(A(row,2),A(row,1),A(row,3)) = Btemp(A(row,2),A(row,1),A(row,3))+1;
end
B=zeros(max(AgeGroup)*2,max(ID));
%every other row (even and odd) are the sexgroups. sexgroup1 is 1,3,5.... sexgroup2 is 2,4,6...
B(1:2:end,:) = Btemp(:,:,1);
B(2:2:end,:) = Btemp(:,:,2);
i think that should do it. or at least gives you a good starting point to correct my 5 min code.
  2 Kommentare
Moe
Moe am 10 Jul. 2014
Joseph! I couldn't run/edit your code, could you edit it?
A= [1,7,1;2,2,1;2,4,2;3,13,2;3,11,2;4,6,2;5,2,2;5,2,2;5,9,1;6,7,1;6,10,2;7,8,1;7,6,2;7,6,2;7,6,1;7,1,1;7,12,2];
Joseph Cheng
Joseph Cheng am 11 Jul. 2014
i got lazy and all you needed to do was switch out AgeGroup and ID with A(:,2) and A(:,3)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by