フィルターのクリア

How to create an an array of unknown size?

2 ビュー (過去 30 日間)
Preetam Pal
Preetam Pal 2015 年 8 月 15 日
コメント済み: per isakson 2015 年 8 月 16 日
Say, I have an array A of size 100 consisting of 0's and 1's. I need to create a smaller array consisting only of the 1's. At start, I don't know the number of 1's in the original array. Basically, I want to create the subset-array element-by-element as I loop over the elements of the original array and checking for 1's.Any help appreciated. Thanks, Preetam
  1 件のコメント
per isakson
per isakson 2015 年 8 月 16 日
"create the subset-array element-by-element as I loop over the elements " &nbsp is that a requirement or a hint?

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

回答 (2 件)

Star Strider
Star Strider 2015 年 8 月 15 日
I’m not quite sure what you want.
Here are two possibilities:
Array = randi([0 1], 1, 100);
OnesArray = Array(Array==1) % Ones Array
ElemArray = find(Array); % Indices Of Ones In ‘Array’

Matt J
Matt J 2015 年 8 月 15 日
編集済み: Matt J 2015 年 8 月 15 日
I'm also not sure what you want, but it might be worth being aware of the nonzeros() command,
B=nonzeros(A);
Or, since you know that A consists only of zeros and ones,
B=ones(nnz(A),1);

カテゴリ

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