fill a vector inside a cycle

2 ビュー (過去 30 日間)
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 9 月 22 日
I have a script where I have to fill a vector like this example:
heads = (1:10);
vector_to_fill = zeros(1,5);
for i = 1 : heads
if condition
vector_to_fill(what index) = heads(i);
end
end
my problem is that I don't know what index putting to that vector_to_fill.how can I slide that vector correctly? If i put the same index as i I leave zeros between element.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 22 日
what do you mean?
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 9 月 22 日
Azz you what index should put to vector_to_fill?

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

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 22 日
clear
heads = 1:10;
k=0
for ii = heads
if rand(1,1)>0.5
k=k+1
vector_to_fill(k) = heads(ii)
end
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by