Assume
a1=[1 5 8];
b1=[6 4 5];
a2=[1 2 3 4 5 6 7 8];
Now I want to create a matching vector with values repeated as follows:
b2=[6 6 6 6 4 4 4 5];
Thanks in advance!

댓글 수: 2

Kevin Chng
Kevin Chng 2018년 10월 16일
Have you tried it?
KSSV
KSSV 2018년 10월 16일
편집: KSSV 2018년 10월 16일
Whats the logic behind b2? If it is a mere repeating...read about repelem and repmat.

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 10월 16일

0 개 추천

b2 = b1(cumsum(ismember(a2,a1)));

댓글 수: 1

Florian
Florian 2018년 10월 16일
Many thanks Andrei, also for answering my other question!

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

추가 답변 (1개)

Bruno Luong
Bruno Luong 2018년 10월 16일
편집: Bruno Luong 2018년 10월 16일

1 개 추천

Another method:
[~,loc] = histc(a2,a1);
b2 = b1(loc)
yet another one
b2 = b1(interp1(a1,1:length(a1),a2,'previous'))

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

질문:

2018년 10월 16일

댓글:

2018년 10월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by