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

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 commentaires

Going to have to give more specifics on how you come up with the decision (definition of "success"). You have 'AA' as 0 but there are combination of entries that match; same for 'AC'. You also have 0 for the same combination of both success and fail--that's not consistent it would seem.
How is your original matrix stored? As a vector of characters?
the cyclist
the cyclist le 14 Août 2015
Modifié(e) : the cyclist le 14 Août 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.)
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.

Connectez-vous pour commenter.

 Réponse acceptée

the cyclist
the cyclist le 14 Août 2015
Modifié(e) : the cyclist le 14 Août 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 commentaires

Here is a slightly neater version, taking advantage of linear indices. It uses the same algorithm.
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);
linearIndex = sub2ind([3,3],loc(ni,1),loc(ni,2));
switch M{ni+1,2}
case 'Success'
successCount(linearIndex) = successCount(linearIndex) + 1;
case 'Failure'
failureCount(linearIndex) = failureCount(linearIndex) + 1;
end
end
successCount
failureCount
Kanakaiah Jakkula
Kanakaiah Jakkula le 15 Août 2015
Modifié(e) : Kanakaiah Jakkula le 15 Août 2015
Sir,
I want to count: (a)how many times (&locations) A appeared after A, or B or C. (b) How many times B appeared after A,orB,or C. (c) How many times C appeared after A,or B,or C. If B appeared after A and it is success, then the count will go to Count_Success matrix, else if it failed then it will go to Count_Fail matrix. I have A~Z combinations (26by26 matrix). How can I define this (as you defined here {'A','B','C','D'})
Change
{'A','B','C'}
to
{'A','B','C', and so on , 'Z'}
and then wherever you see the number 3 in my code, replace it with 26.
Kanakaiah Jakkula
Kanakaiah Jakkula le 15 Août 2015
Modifié(e) : Kanakaiah Jakkula le 15 Août 2015
Sir,
Instead of defining like {'A','B','C', and so on , 'Z'}, Can I define it an array (and read using xlsread)like Names_array? before the for loop begins. I want to store it in array because I just mentioned here as A,B,C..., but in reality each letter representslike:NSF4_TTD_55+_1002cm_hh_2122N_2022N_3000N_1899uM_IND, there are more than 100. That is why I want read from file, and store in array form. So, instead of defining {'A','B','C', and so on , 'Z'}, but I want to call from array. Kindly help.
Many many thanks.
Yes, that is possible.
{'A', ..., 'Z'}
is a cell array. You should be able to read in a file, and then store it in a cell array. (I don't know if xlsread will do that, and I cannot really test that because xlsread doesn't fully function on Macs.)
One easy way is to use the "Import Data" tool in the command window.
If you struggle to do this particular part, I suggest you open a new question about that. (And I would appreciate if you "Accept" my solution here, which I believe solved your original problem.)
Sir,
I can store in array form (i.e, Names_array), it is cell array. After that I called here:
[~,loc] = ismember([M(1:end-1,1) M(2:end,1)],{Names_array}');
But it giving me the error as below:
Error in ==> Matrix_successFailCount at 13
[~,loc] = ismember([M(1:end-1,1) M(2:end,1)],{Names_array}');.
Please help I can I call from Names_array in place of {'A','B','C','D'}. Many thanks,
[~,Names_array]=xlsread('HTDNames.csv');
My code is as below:
M = {
'A' 'Success'
'B' 'Failure'
'B' 'Success'
'C' 'Success'
'A' 'Success'
'B' 'Success'
'C' 'Success'};
% ABC_matrix={'A''B''C'};
[~,Names_array]=xlsread('HTDNames.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','D'});
[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'
failureCount(linearIndex) = failureCount(linearIndex) + 1;
end
end
successCount;
failureCount;
Your variable Names_array is already a cell array, so you do not need to enclose it inside {...}, because then it is a cell array enclosing another cell array. That is what is confusing MATLAB. Trying modifying this:
[~,loc] = ismember([M(1:end-1,1) M(2:end,1)],{Names_array}');
to this:
[~,loc] = ismember([M(1:end-1,1) M(2:end,1)],Names_array');
Sir,
It works. Many many thanks sir.
Finally, for the same code, I have the below matrix:I added third column here, which are actually the scores.
A Success 20
B Failure 20
B Success 23
C Success 21
A Success 31
B Success 20
C Success 4
A Success 6
As I noted the Success counts and failure counts, Now I want to take the average of the scores for the counts (average of total counts for each combination). Success_count:
0 1 0
0 1 2
2 0 0
Failure_count
0 1 0
0 0 0
0 0 0
For instance, In Success_count row2, column3 the count is 2, because C appears after B at row4&row7, and the corresponding scores are 21&4. So, the average of 21+4=12.5(and this is the average of scores of the success counts). Please help me sir, I tried but I did not get. I used below code:
clc;
clear all;
close all;
M = {
'A' 'Success'
'B' 'Failure'
'B' 'Success'
'C' 'Success'
'A' 'Success'
'B' 'Success'
'C' 'Success'
'A' 'Failure'};
Scores=[20;20; 23;21;31;20;4;6];
% ABC_matrix={'A''B''C'};
% [~,Names_array]=xlsread('HTDNames.csv');
% [Scores,M]=xlsread('All_Data_ABCDEF.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'
failureCount(linearIndex) = failureCount(linearIndex) + 1;
end
end
successCount;
failureCount;
AvegScores_success: Should be,
0 20 0
0 23 12.5
18.5 0 0
and
AvegScores_failure: Should be,
0 20 0
0 0 0
0 0 0
I am very thank full you,
clc;
clear all;
close all;
M = {
'A' 'Success'
'B' 'Failure'
'B' 'Success'
'C' 'Success'
'A' 'Success'
'B' 'Success'
'C' 'Success'
'A' 'Failure'};
Scores=[20;20; 23;21;31;20;4;6];
% ABC_matrix={'A''B''C'};
% [~,Names_array]=xlsread('HTDNames.csv');
% [Scores,M]=xlsread('All_Data_ABCDEF.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,successSum,failureSum] = 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;
successSum(linearIndex) = successSum(linearIndex) + Scores(ni+1)
case 'Failure'
failureCount(linearIndex) = failureCount(linearIndex) + 1;
failureSum(linearIndex) = failureSum(linearIndex) + Scores(ni+1)
end
end
successCount
failureCount
successMean = successSum./successCount
failureMean = failureSum./failureCount
Where there are no counts, this gives the mean as "Nan" (because 0/0 is undefined). If you don't like that, then you can do
successMean(isnan(successMean)) = 0;
failureMean(isnan(failureMean)) = 0;
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 documentation for switch is pretty clear on how to handle this:
case {'Failure','Abort','Manual'}

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by