필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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
dpb
dpb 2016년 5월 21일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 21일
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)

추가 답변 (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