random elements from specific rows in matrix
이전 댓글 표시
Hi,
I have a 2x10 matrix and I want to select a random element from each row. I have found how to select random elements from the matrix as a whole but can't narrow it down.
Thanks,
Richard
채택된 답변
추가 답변 (1개)
Derek O'Connor
2011년 4월 5일
function rrelems = RandRowElems(A)
% USE: m = 10; n = 10^6;
% A = ceil(100*rand(m,n));
% tic;relems = RandRowElems(A),toc
[m,n] = size(A);
rrelems = zeros(m,1);
for i = 1:m
rcidx = ceil(rand*n);
rrelems(i,1) = A(i,rcidx);
end
댓글 수: 2
Vinita
2012년 7월 24일
What if I have a 40X40 matrix and i want to select a random number from this matrix.
Derek O'Connor
2012년 7월 26일
I presume you mean to pick a number randomly from a 40x40 matrix:
Pick two random integers r and c in [1:40]; A(r,c) is the answer.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!