필터 지우기
필터 지우기

how can i select a random number from a column of a matrix? and then how to find the location of that number?

조회 수: 6 (최근 30일)
I have a matrix (4x3) and I need to select a value from second column and find the corresponding row number.
For example, A = [11 24 31; 18 19 27; 16 21 34; 23 37 33 28]
Could someone help me?

답변 (1개)

Iddo Weiner
Iddo Weiner 2018년 4월 1일
If you use a random number to index the row you want to extract, you will already have what you need:
A = randi(10,[4,3]);
rand_row = randi(size(A,1)) %get random row
rand_number = A(rand_row,2) %get your rand number form the 2nd column
  댓글 수: 5
Rajeev Kant
Rajeev Kant 2018년 4월 3일
Thank you guys.
What I am looking for the output will be any random element from the second column i.e. anyone of 24,19,21 and 37 (please discard 28 of the 4th row, apology for typo mistake)
and then the corresponding row number i.e.
for 24, row = 1
for 19, row = 2 and so on.
Guillaume
Guillaume 2018년 4월 3일
Choosing a random number from the 2nd column and getting the corresponding row is exactly equivalent to choosing a random row and getting the corresponding number in the 2nd column. Iddo showed you how to do the latter, so again you'll have to explain why it didn't work for you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by