필터 지우기
필터 지우기

Create a new array from an intersection of two previous arrays

조회 수: 1 (최근 30일)
luca
luca 2019년 10월 3일
편집: Stephen23 2019년 10월 3일
Hi given the following arrays
A = [1 1 1 1 4 4 4 4 6 6 6 6 6 6]
B = [2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7 7 7 8 8 8 8 9 9 9 9]
I want to create a new vector C that is the intersection of A and B
C = [1 2 1 2 1 2 1 2 4 2 4 2 4 2 4 2 6 2 6 2 6 7 6 7 6 7 6 7 7 7 7 7 8 8 8 8 9 9 9 9 ]
So C is created by insert one value of B after one value of A, till the end.

채택된 답변

Stephen23
Stephen23 2019년 10월 3일
편집: Stephen23 2019년 10월 3일
>> A = [1 1 1 1 4 4 4 4 6 6 6 6 6 6];
>> B = [2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7 7 7 8 8 8 8 9 9 9 9];
>> N = min(numel(A),numel(B));
>> C = [reshape([A(1:N);B(1:N)],1,2*N),A(N+1:end),B(N+1:end)]
C =
1 2 1 2 1 2 1 2 4 2 4 2 4 2 4 2 6 2 6 2 6 7 6 7 6 7 6 7 7 7 7 7 8 8 8 8 9 9 9 9

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by