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?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
the cyclist
el 13 de Feb. de 2023
Editada: the cyclist
el 13 de Feb. de 2023
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?
Respuestas (1)
the cyclist
el 13 de Feb. de 2023
Editada: the cyclist
el 13 de Feb. de 2023
This is easiest using linear indexing. (See the section on linear indexing on this page, for example.)
% Input matrix
M = magic(4)
% 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)]
If you really need the (i,j)-indexing, you can get it:
[i,j] = ind2sub(size(M),randomIdx)
0 comentarios
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!