If a=[1 2 3], b=[10 20 30], how to create c=[10 20 20 30 30 30]?

조회 수: 5 (최근 30일)
Joshua
Joshua 2014년 7월 14일
편집: Roger Stafford 2014년 7월 14일
The b matrix i'th element is repeated the number of times the given number in 'a' matrix and placed in matix 'c'. Also if a=[0 1 2], b=[10 20 30] how to get c=[20 30 30]??

채택된 답변

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2014년 7월 14일
c = cell2mat(arrayfun(@repmat,b,ones(size(a)),a,'uni',0));

추가 답변 (2개)

Roger Stafford
Roger Stafford 2014년 7월 14일
편집: Roger Stafford 2014년 7월 14일
Another way:
t = accumarray(cumsum([1,a]).',1);
c = b(cumsum(t(1:end-1)));
  댓글 수: 2
Jan
Jan 2014년 7월 14일
I assume you mean "t = " instead of "t - "
Roger Stafford
Roger Stafford 2014년 7월 14일
Oops! Yes, thank you Jan.

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


Jan
Jan 2014년 7월 14일
For large array this is efficient: FEX: RunLength
c = RunLength(a, b)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by