There is a matrix A (size:12x1000)
How can I select 5 random entries out of 1000( from a matrix of size 12x1000), with each randomly selected value contains all the 12 rows from the original matrix. And store it into another matrix of size 12x5.
Can anyone help me with the code.

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 10 日

1 投票

You can use randperm() in a for-loop
M = rand(12, 1000);
rand_elements = zeros(12, 5);
for i = 1:12
idx = randperm(1000, 5);
rand_elements(i,:) = M(i, idx);
end

その他の回答 (2 件)

Stephan
Stephan 2020 年 11 月 10 日
編集済み: Stephan 2020 年 11 月 10 日

1 投票

% Test data to play with
A = rand(12,1000);
% Preallocate
idx = zeros(size(A,1),5);
% 5 unique random indices for every row:
for k = 1:size(A,1)
idx(k,:) = randperm(size(A,2),5);
end
% result
B = A(idx)
Bruno Luong
Bruno Luong 2020 年 11 月 10 日

1 投票

rcols = randperm(size(A,2),5);
B = A(:,rcols)

2 件のコメント

Stephan
Stephan 2020 年 11 月 10 日
I think same indexes for different rows are allowed - arent they?
Bruno Luong
Bruno Luong 2020 年 11 月 10 日
I just interpret the question like this. I don't know if it's what OP wants or not.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by