フィルターのクリア

How to construct this vector without loop?

2 ビュー (過去 30 日間)
Ming
Ming 2013 年 10 月 23 日
編集済み: Jos (10584) 2013 年 10 月 23 日
Hi, everyone:
I was wondering, is it possible to construct following vector without for loop?
A=[1 2 3 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 .....]
or equivalently A=[1:(3*1), 1:(3*2), 1:(3*3), 1:(3*4), ...., 1:(3*n)]
Many thanks!

採用された回答

Jos (10584)
Jos (10584) 2013 年 10 月 23 日
編集済み: Jos (10584) 2013 年 10 月 23 日
Here is flexible version not using cell2mat:
n = 4 ; % user specified
V = 3 ; % as in the example -> [1:V 1:2*V ... 1:n*V]
ix = V*[1:n]
A = ones(1,sum(ix))
ix =ix(1:n-1)
A(cumsum(ix)+1)=-ix+1
A = cumsum(A) % result

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 23 日
編集済み: Azzi Abdelmalek 2013 年 10 月 23 日
A=[1 2 3 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9]
cell2mat(arrayfun(@(x) 1:3*x,A,'un',0))

Vivek Selvam
Vivek Selvam 2013 年 10 月 23 日
編集済み: Vivek Selvam 2013 年 10 月 23 日
vec = 3*(1:n);
A = cell2mat(arrayfun(@(x) 1:x,vec,'UniformOutput',0));
or
vec = 1:n;
A = cell2mat(arrayfun(@(x) 1:3*x,vec,'UniformOutput',0));

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by