Effacer les filtres
Effacer les filtres

How can I mix up "blocks" of elements of 2 vectors and put them in a new vector?

2 vues (au cours des 30 derniers jours)
How can I mix up "blocks" of elements of 2 vectors and put them in a new vector?
for example:
if I have vectors A and B:
A = [ 2; 3; 4; 5; 4; 6; 2; 1]
B = [33; 34; 55; 34; 54; 55; 56; 43]
I want to divide vector A into blocks with different sizes, where the maximum number of elements in the block is 4 and the minimum is 1:
A1 = [2 ; 3 ; 4 ;5]
A2 = [4]
A3 = [6 ; 2]
A4 = [1]
And the same for vector B, where the maximum number of elements in the block is 4 and the minimum is 1:
B1 = [33]
B2 = [34 ; 55 ; 34]
B3 = [54 ; 55 ; 56 ; 43]
Then I want to define a new vector that contains these blocks:
Say:
C = [A1 ; B1 ; B2 ; A2 ; A3 ; B3 ; A4]
I can do the above steps for small number of elements (like the above example), but what if the number of elements in each vector is 1000? How can I divide the blocks randomly and define a new variable (C) that contains these blocks?
  2 commentaires
dpb
dpb le 21 Mai 2016
Sampling with or without replacement within each subset?
Hint: Don't need to build the other arrays, simply define the number of groups and size of each group and concatenate the selection of each.
Precise code depends up the answer to the above question plus how the number and size of the groups is to be ascertained.
Sara Al Shamsi
Sara Al Shamsi le 23 Mai 2016
thank you for the hint.

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 21 Mai 2016
A=randi(10,1,30) % -----Example-------
n=numel(A)
id=randi(4,1,n);
ii=cumsum(id);
jj=find(ii<=n);
ss=jj(end);
e=n-ii(ss)
part=id(jj)
part(end)=part(end)+e
out=mat2cell(A',part,1)

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