Clear Filters
Clear Filters

sequence/pattern problem

1 view (last 30 days)
GMDI
GMDI on 8 Feb 2021
Commented: GMDI on 9 Feb 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.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Feb 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 Comment
GMDI
GMDI on 9 Feb 2021
Thank you so much for your help. I really appreciate it. :)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!