Randomly select a number from a column of a stored matrix, stores the respective row

2 ビュー (過去 30 日間)
Akana Juliet
Akana Juliet 2021 年 6 月 17 日
コメント済み: Akana Juliet 2021 年 6 月 17 日
I am a novice MATLAB user, so please bear with me. I have a 256x5 double value stored, and the first column is just the numbers 0-255. The next 4 columns are a sequence of 4 numbers in a specific order.
I am trying to write a code that selects a number from Column 1 at random(0-255), and then subsequently saves the respective 4 element array/vector (of the row selected). How do you think I can accomplish this?


Sky Sartorius
Sky Sartorius 2021 年 6 月 17 日
You can use randi to randomly generate the index you need,
rowInd = randi(256,1);
then use that index to pull out the row from your matrix M:
fourElementVector = M(rowInd,2:end);
Of course this can also be done in one step:
  1 件のコメント
Akana Juliet
Akana Juliet 2021 年 6 月 17 日
You are a genius, works like a charm. Thank you so much!


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by