フィルターのクリア

How to sample uniformly elements on a matrix and store the location information , i.e, (i,j) i-th row and j-th col , in a data type like list in Python?

2 ビュー (過去 30 日間)
Bowen
Bowen 2023 年 2 月 13 日
編集済み: the cyclist 2023 年 2 月 13 日
How to sample uniformly elements on a matrix and store the location information , i.e, (i,j) i-th row and j-th col , in a data type like list in Python? Thanks!
  1 件のコメント
the cyclist
the cyclist 2023 年 2 月 13 日
編集済み: the cyclist 2023 年 2 月 13 日
Do you want to sample with replacement, or without? (In other words, can you select the same element multiple times?)
Do you have access to the Statistics and Machine Learning Toolbox?

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

回答 (1 件)

the cyclist
the cyclist 2023 年 2 月 13 日
編集済み: the cyclist 2023 年 2 月 13 日
This is easiest using linear indexing. (See the section on linear indexing on this page, for example.)
% Input matrix
M = magic(4)
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
% Number of elements desired
n = 3;
% Generate a random indices into the matrix.
% (This particular method may generate repeats.)
randomIdx = randi(numel(M),3,1);
% Display the index and the randomly selected element
[randomIdx, M(randomIdx)]
ans = 3×2
8 14 14 8 2 5
If you really need the (i,j)-indexing, you can get it:
[i,j] = ind2sub(size(M),randomIdx)
i = 3×1
4 2 2
j = 3×1
2 4 1

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by