Generate a vector "b" from an already defined vector "B" several times but taking different elements of "A" each time

1 回表示 (過去 30 日間)
I have to generate a vector "b" from an already defined vector "B" but taking different set of values each time. The length of the new vector "b" should be the same each time, but the indices of values that it takes from "B" should be different each time.
Lets say that B = 1x4425. I have to define new vector "b" 15 times but each time its length has to be 1x295. Also the indices of elements of "B" that "b" takes have to be different for each time that "b" is generated, there shouldn't be any repetitions for any of the elements in "b". Also, every time that "b" is generated I have to remember the indices of elements that are taken from "B".
Any advice on how to write this is more than welcome.
Thank you!

採用された回答

Jeff Miller
Jeff Miller 2017 年 11 月 27 日
idx=reshape(randperm(4425),15,295);
b = reshape(B(idx(:)),15,295);
The 15 rows of b are the 15 new vectors, and idx holds the index in B of each element in each vector.
  1 件のコメント
Stephen23
Stephen23 2017 年 11 月 27 日
編集済み: Stephen23 2017 年 11 月 27 日
Note that converting idx to column and the second reshape are superfluous.

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

その他の回答 (2 件)

Stephen23
Stephen23 2017 年 11 月 27 日
編集済み: Stephen23 2017 年 11 月 27 日
>> B = rand(1,4425);
>> out = B(reshape(randperm(4425),15,295));
Each row of out is a random vector of elements of B. Each element of B appears only once in out.

EB
EB 2017 年 11 月 28 日
Thank you for you answers. Because I needed to remember the indices of the elements that are taken from B, I have accepted the first answer.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by