Generating a long vector from two other vectors
4 ビュー (過去 30 日間)
古いコメントを表示
Shoaibur Rahman
2015 年 7 月 22 日
コメント済み: Shoaibur Rahman
2015 年 7 月 22 日
I got stuck with a (may be) simple problem. Here are two vectors, like:
a = [20 13 24 ...];
b = [3 2 4 ...];
How can I create a new vector that contains 3 20s, 2 13s, 4 24s, and so on? (Not using any loop). The output will look like as shown in c:
c =
20 20 20 13 13 24 24 24 24 ...
0 件のコメント
採用された回答
Azzi Abdelmalek
2015 年 7 月 22 日
編集済み: Azzi Abdelmalek
2015 年 7 月 22 日
a = [20 13 24 ];
b = [3 2 4 ];
out=cell2mat(arrayfun(@(x,y) repmat(x,1,y),a,b,'un',0))
or with a for loop
a = [20 13 24 ];
b = [3 2 4 ];
out=zeros(1,numel(a))
c=[ 0 cumsum(b)]
for k=1:numel(a)
out(c(k)+1:c(k+1))=a(k)*ones(1,b(k))
end
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!