Let's assume my vector is
A = [1 2 3 7 9 10 45 93 122 150]';
I want to put zeros in between these numbers.
A = [1 2 3 0 0 0 7 0 9 10 0 0 .....]';

 採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 1 月 20 日
編集済み: Andrei Bobrov 2017 年 1 月 20 日

0 投票

One of ways:
A = [1 2 3 7 9 10 45 93 122 150]'
ii = randi(3,numel(A),1);
t = cumsum(ii) < numel(A);
ii = ii(t);
z = arrayfun(@(x)zeros(x,1),ii(1:end-1),'un',0);
C = mat2cell(A(1:sum(ii)),ii,1);
out = [C';[z',{[]}]];
out = cat(1,out{:});

3 件のコメント

Deepti Ranjan Majhi
Deepti Ranjan Majhi 2017 年 1 月 20 日
Thank you for your reply. But, I want a vector of length 150X1.
Andrei Bobrov
Andrei Bobrov 2017 年 1 月 20 日
Please!
Deepti Ranjan Majhi
Deepti Ranjan Majhi 2017 年 1 月 20 日
Thank you for reply. I sort out the problem.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2017 年 1 月 20 日
編集済み: Jan 2017 年 1 月 20 日

0 投票

A = [1 2 3 7 9 10 45 93 122 150]';
B = zeros(150, 1);
Index = [true(1, numel(A)), false(1, numel(B) - numel(A)];
Index = Index(randperm(numel(Index));
B(Index) = A;
Now the result is filled at random places by zeros such that the total length is 150.
[EDITED] Easier:
A = [1 2 3 7 9 10 45 93 122 150]';
B = zeros(150, 1);
Index = randperm(numel(B), numel(A));
B(Index) = A;

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2017 年 1 月 20 日

編集済み:

Jan
2017 年 1 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by