Effacer les filtres
Effacer les filtres

sequence/pattern problem

1 vue (au cours des 30 derniers jours)
GMDI
GMDI le 8 Fév 2021
Commenté : GMDI le 9 Fév 2021
Hi, I have a problem.
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ];
From above A, I want B like below :
B=[1 2 3 4 8 9 10 11 15 16 17 18 ];
Now, if my A is like below:
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
Then B will be like below:
B=[1 2 3 4 8 9 10 11 15 16 17 18 22 23 24];
Which means. first four elements of A will be in the B; then the next three elements of A will not be in B; then next 4 elements of A will be in B; and so on so forth..
Last few elements of A will be / will not be in the B depend on if they are in the sequence or not.
I tried to explain the case study above. Please let me know if something is not clear.
Thanks in advance.

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Fév 2021
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ];
B1 = chop43(A(1:20))
B1 = 1×12
1 2 3 4 8 9 10 11 15 16 17 18
B2 = chop43(A)
B2 = 1×15
1 2 3 4 8 9 10 11 15 16 17 18 22 23 24
function B = chop43(A)
full_blocks = floor(length(A)/7);
blocksizes = repmat([4 3], 1, full_blocks);
leftover = length(A) - full_blocks*7;
if leftover > 0 & leftover <= 4
blocksizes(end+1) = leftover;
elseif leftover >= 5
blocksizes(end+1:end+2) = [4, leftover-4];
end
blocks = mat2cell(A, 1, blocksizes);
B = [blocks{1:2:end}];
end
  1 commentaire
GMDI
GMDI le 9 Fév 2021
Thank you so much for your help. I really appreciate it. :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices 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