필터 지우기
필터 지우기

how to select a random number from the existing matrix in each column

조회 수: 3 (최근 30일)
PRANAY DISHAN
PRANAY DISHAN 2018년 2월 7일
댓글: PRANAY DISHAN 2018년 2월 7일
Dear friends, I need to select a random number from each column from the existing matrix to create a new row as shown below: I'm new to matlab. So please help me with this. Thank you.
A=[1 2 3 4;5 6 7 8;9 10 11 12]
Anew=[5 2 7 12]

답변 (2개)

Roger Stafford
Roger Stafford 2018년 2월 7일
편집: Roger Stafford 2018년 2월 7일
[m,n] = size(A);
Anew = A(randi(m,[1,n])+m*(0:n-1));

per isakson
per isakson 2018년 2월 7일
편집: per isakson 2018년 2월 7일
It's smarter to use a for-loop
>> A
A =
1 2 3 4
5 6 7 8
9 10 11 12
>> [nrow,ncol] = size(A);
>> ix = randi([1,nrow],[1,ncol])
ix =
3 1 3 1
>> A(sub2ind(size(A),ix,1:ncol))
ans =
9 2 11 4

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by