필터 지우기
필터 지우기

Insert values of a vector between two elements of another vector and at the end of it.

조회 수: 10 (최근 30일)
I have a vector a=[0.2 2 3 4] and I have another vector that is b=[0.1 1 2 5] so how can I insert each element of b between elements of a and the last element of b after the last element of a?
So in effect, take 0.1 from b, insert it between 0.2 and 2 in a, take 1 from b and insert it between 2 and 3 in a, take 2 from b and insert it between 3 and 4 and take 5 from b and insert it after 4
so after this process, a new matrix say c will be c=[0.2 0.1 2 1 3 2 4 5]
The logic I would assume would be used is take the last element of b and remove it, the follow a process to insert the remaining values of b into a then in that new vector, insert the last value at the end. Idk, any help is greatly appreciated, thanks.

채택된 답변

Honglei Chen
Honglei Chen 2018년 9월 22일
편집: Honglei Chen 2018년 9월 22일
Is it just
c = [a;b]
c = c(:)
Did I understand it correctly?
HTH
  댓글 수: 4
Vishan Gupta
Vishan Gupta 2018년 9월 22일
Wow it worked!! thanks so much, was trying to figure this out for ages, could you please explain how this works, the logic behind it? Thanks alot!
Honglei Chen
Honglei Chen 2018년 9월 22일
You are just trying to interleaving the two vectors. When MATLAB converts a matrix to vector using colon, it connects columns together, that's why you can use this trick to interleaving the two.
HTH

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matched Filter and Ambiguity Function에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by