Select random elements from three consecutive columns in a big matrix?
10 ビュー (過去 30 日間)
古いコメントを表示
Hello all,
I have a 12 by 12 matrix, and I want to select 10 random elements. The elements should be from only three consecutive columns.
For example, I want to use 10 random elements from columns (1, 2, 3) or they could be from columns (2, 3, 4) or columns(3, 4, 5) and so on.
Any help please.
Thanks
3 件のコメント
Guillaume
2019 年 4 月 5 日
編集済み: Guillaume
2019 年 4 月 5 日
Please do not ask the same question twice.
Duplicate of https://www.mathworks.com/matlabcentral/answers/454534-select-elements-from-only-three-consecutive-columns
And it looks like you delete your question once answered, which is not going to go down well with us (and will be restored anyway).
回答 (2 件)
Walter Roberson
2019 年 4 月 5 日
N = 10;
[r, c] = size(YourMatrix);
start_col = randi([1, c-2]);
chosen = YourMatrix(start_col * r - 1 + randi([1 3*r], 1, N));
Jos (10584)
2019 年 4 月 5 日
編集済み: Jos (10584)
2019 年 4 月 5 日
M = magic(5) % input data
N = 4 % number of selections
nC = 3 % number of consecutive columns
% engine
szM = size(M)
r = randi(szM(1), N, nC) % N x 3 random rows
c = randi(szM(2) - nC + 1, N, 1) % N random first columns
c = c + [0:nC-1] % add consecutive columns
R = M(sub2ind(szM, r, c)) % index into M
参考
カテゴリ
Help Center および 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!