Effacer les filtres
Effacer les filtres

How to find out the maximum number of occurrences of the sequences inside the cell array beginning with particular digit?

3 vues (au cours des 30 derniers jours)
% Suppose I have a cellarray
C = {[1; 2; 3]; [2; 1; 3, 4]; [3; 1; 2]; [1; 2]}; % where any digit won't repeat in the individual cell.
% I was trying this code
for i = 1:3
for j = 1:3
for k = 1:3
if(i ~= j && i ~= k && j ~= k) % to prevent repetition
sequence_check = [i; j; k];
n = sum(cellfun(@(x) ~isempty(strfind(x.',sequence_check.')), C));
t = table(i, j, k, n)
end
end
end
end
% which gives me possible sequences (using 1,2 and 3) | number of occurrences
123 | 1
132 0
213 1
231 0
312 1
321 0
% I need to find out the maximum number of occurrences of the sequences inside the cell array beginning with 1 or 2 or 3 individually.
% Expected output:
123 | 1
213 1
312 1

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 19 Nov 2018
C = {[1; 2; 3]; [2; 1; 3; 4]; [ 4; 2; 1; 3];[3; 1; 2]; [1; 2]};
a = perms(1:3);
[~,j2] = cellfun(@(x)ismember(a,x),C,'un',0);
B = cat(3,j2{:});
n = sum(all(diff(B,1,2) == 1,2) & B(:,1,:) > 0,3);
lo = n > 0;
out = [a(lo,:),n(lo)];
  2 commentaires
Md Shahidullah Kawsar
Md Shahidullah Kawsar le 19 Nov 2018
If I change the C and a = perms(1:10); the runtime is quite high. and If I write a = perms(1:20); it shows the maximum variable size allowed by the program is exceeded.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by