how to generate random points in a matrix
Mostrar comentarios más antiguos
hi , I have a problem here and need some help; suppose here we have a m*n matrix and we want to generate some points in a matrix(or in an image) randomly. how can I generate these points ? thanks in advance ; any comments will be appreciated .
1 comentario
Jan
el 6 de Nov. de 2011
What does this exactly mean: Generate some points in a m*n matrix? Do you want some random indices in the range 1:m and 1:n? Or do the values of the matrix matter?
Respuestas (3)
Wayne King
el 6 de Nov. de 2011
If you have the Statistics Toolbox, you can use randsample() I'll assume a 256x256 matrix for this example.
indices = randsample(256^2,50);
[I,J] = ind2sub([256 256],indices);
The above gives you the row and column indices. However you can just use the output of randsample(), the linear indices to fill and extract values from the matrix.
X = randn(256,256);
X(indices) = 0;
2 comentarios
Amin
el 7 de Nov. de 2011
Wayne King
el 7 de Nov. de 2011
yes, I don't have your data, so I just created an example with X as the data matrix
X = randn(256,256);
Image Analyst
el 6 de Nov. de 2011
Amin:
Run this code and see if it's what you are looking for.
% Generate a totally black image to start with.
m = zeros(300, 400, 'uint8');
% Generate 1000 random locations.
numRandom = 1000;
linearIndices = randi(numel(m), 1, numRandom);
% Set those lcoations to be white.
m(linearIndices) = 255;
% Display it. Random locations will appear white.
image(m);
colormap(gray);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
caption = sprintf('%d Random Pixels are Set to White', numRandom);
title(caption, 'FontSize', 30);
2 comentarios
Amin
el 7 de Nov. de 2011
Image Analyst
el 7 de Nov. de 2011
Change the randi line to this and it will work:
linearIndices = ceil(numel(m) * rand(1, numRandom));
Amin
el 7 de Nov. de 2011
1 comentario
Walter Roberson
el 7 de Nov. de 2011
fix(21025*rand) will result in 0, 1 in 42050 times.
fix(190*rand) will result in 0, 1 in 380 times.
There was a good reason why Image Analyst used ceil() instead of fix()
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!