random elements from specific rows in matrix

1 回表示 (過去 30 日間)
darksideofthemoon101
darksideofthemoon101 2011 年 4 月 5 日
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

採用された回答

Titus Edelhofer
Titus Edelhofer 2011 年 4 月 5 日
Randperm is a simple helper for this:
X = [1:10; 11:20];
idx1 = randperm(10);
idx2 = randperm(10);
entries = [X(1,idx1(1)); X(2, idx2(1))]
Titus

その他の回答 (1 件)

Derek O'Connor
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
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
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.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by