How to extract the all combinations of array (permutations combinations)

4 views (last 30 days)
Hi,
I have a matrix as below:
A Success
B Failure
C Success
A Success
B Success
C Success
I want to count how many time B appeared after A or A appeared after A or C appeared after B (for example: i-1 should be A, and i should B: so it counted as AB). The resultant combinations are as shown below: So, again I want to count how many time A appeared after A, B appeared after A, B appeared after B, C appeared after B (or C,or A,or D)etc.
AA AB AC
BA BB BC
CA CB CC
My out put should be: Out_sucess (only succeefull combinations):
0 1 0
0 0 2
1 0 0
Out_Fail(Failed combinations):
0 1 0
0 0 0
0 0 0
Kindly help how do I get these out puts
  4 Comments
the cyclist
the cyclist on 14 Aug 2015
Edited: the cyclist on 14 Aug 2015
@dpb, I think he/she is only looking at "nearest neighbor" pairs, and basing the classification on the success/failure of the 2nd member of the pair. (At least, this gives the quoted result.)
Kanakaiah Jakkula
Kanakaiah Jakkula on 15 Aug 2015
Sir,
The meaning of resultant matrix I shown above is AA means A appeared after A. AB means B appeared after A like in row2. BA means A appeared after B. BC means C appeared after B as in row3 etc. I don't mind B appeared after A or after B itself). I just want to count how it appeared (that is after A or after B, or C) but how many time B appeared after A, and B appeared after B it self. Same as for A &C. That's why I want to count as below matrix:
Count_Matrix:
0 2 0
0 0 2
1 0 0
For instance, in the Count_Matrix(1,2) the count is 2, because B appeared after A twice at row2 &row5. Count_Matrix(3,1) the count is 1, because A appeared after C once at row4 etc.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 14 Aug 2015
Edited: the cyclist on 14 Aug 2015
Perhaps not the most efficient way, but here is a very pedantic way:
M = {
'A' 'Success'
'B' 'Failure'
'C' 'Success'
'A' 'Success'
'B' 'Success'
'C' 'Success'};
[~,loc] = ismember([M(1:end-1,1) M(2:end,1)],{'A','B','C'});
[successCount,failureCount] = deal(zeros(3));
for ni = 1:size(loc,1);
switch M{ni+1,2}
case 'Success'
successCount(loc(ni,1),loc(ni,2)) = successCount(loc(ni,1),loc(ni,2)) + 1;
case 'Failure'
failureCount(loc(ni,1),loc(ni,2)) = failureCount(loc(ni,1),loc(ni,2)) + 1;
end
end
successCount
failureCount
  11 Comments
Kanakaiah Jakkula
Kanakaiah Jakkula on 21 Aug 2015
Sir,
Thank you very much. It works, I encountered one problem. Here, the failure cases are separated into three categories like: 'Failure', 'Abort', 'Manual' all these belongs to failure only, but different naming. I modified the code using OR symbol (), but it gives the following error: ??? Operands to the and && operators must be convertible to logical scalar values.
Error in ==> Matrix_successFailCount_Mod at 36 case 'Failure'||'Abort'||'Manual'
My code is below: And May I seek your kind help sir, Many thanks in advance.
clc;
clear all;
close all;
M = {
'A' 'Success'
'B' 'Failure'
'B' 'Success'
'C' 'Success'
'A' 'Success'
'B' 'Success'
'C' 'Success'
'A' 'Abort'
'A' 'Manual'};
Scores=[20;20; 23;21;31;20;4;6;24];
ABC_matrix={'A''B''C'};
% [~,Names_array]=xlsread('HTDNames2.csv');
% [Scores,M]=xlsread('All_Data_ABCDEF3.csv');
% [~,loc] = ismember([M(1:end-1,1) M(2:end,1)],Names_array');
[~,loc] = ismember([M(1:end-1,1) M(2:end,1)],{'A','B','C'});
[successCount,failureCount] = deal(zeros(3));
for ni = 1:size(loc,1);
linearIndex = sub2ind([3,3],loc(ni,1),loc(ni,2));
switch M{ni+1,2}
case 'Success'
successCount(linearIndex) = successCount(linearIndex) + 1;
case 'Failure'||'Abort'||'Manual'
failureCount(linearIndex) = failureCount(linearIndex) + 1;
end
AvgScores_S=mean(successCount);
AvgScores_F=mean(failureCount);
end
successCount;
failureCount;
the cyclist
the cyclist on 21 Aug 2015
Edited: the cyclist on 21 Aug 2015
The documentation for switch is pretty clear on how to handle this:
case {'Failure','Abort','Manual'}

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!