sequence/pattern problem

1 次查看(过去 30 天)
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.

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by