On May 13, 6:18 pm, "Safa " <enxs...@nottingham.ac.uk> wrote:
> Dear ImageAnalyst
> Thanks very much for your reply. You've given me a few ideas and I've tried to read also some of your earlier posts on this subject. I also found this function filehttp://www.mathworks.com/matlabcentral/fileexchange/7309
> although not sure how I am going to use it, as it is quite a lengthy m file and I need time to study it.
>
> I think what I am looking for is a combination of the above two methods. With the code you've suggested, you get the following, and to me the selection process doesn't seem to be following any pattern.
>
> m =
> Columns 1 through 11
> 5 4 8 8 2 5 5 7 8 8 3
> Columns 12 through 20
> 7 7 2 2 5 10 4 6 3
> randomIndexes =
> Columns 1 through 11
> 8 9 17 14 18 12 2 10 16 20 3
> Columns 12 through 20
> 7 19 4 1 13 11 5 15 6
> randomlyPickedElements =
> Columns 1 through 11
> 7 8 10 2 4 7 4 8 5 3 8
> Columns 12 through 20
> 5 6 8 5 7 3 2 2 5
>
> I have a curve, I want to select numbers at random that follows the curve and I think your second suggestion is better but probably more complicated. Could I ask you for an example? I.e. some code that takes a 1D array of Y values, compute PDF, CDF, invert then apply uniform distribution. Would really appreciate that, I can then understand what commands in Matlab I need to use. Thanks in advance for your assistance.

Safa:
Actually I thought you probably wanted the first thing I suggested,
but since you asked, here is a demo where I generate numbers from a
Rayleigh distribution using the well known method. Pay attention to
the IMPORTANT note and make sure you fix the line breaks that will get
introduced. Just chase down the syntax errors or follow the red lines
on the right margin of your editor in MATLAB.
% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
% Script to generate random numbers drawn from a Rayleigh
distribution.
% See Wikipedia article for definition of the Rayleigh
% probability density function and cumulative distribution function:
% http://en.wikipedia.org/wiki/Rayleigh_distribution
% Uses the well known method of inverting the CDF of the desired
probability
% function to get a function that can generate random numbers drawn
% from that desired distribution function,
% given as input numbers that have been drawn from a uniform
distribution.
% Reference for that method:
% http://en.wikipedia.org/wiki/Inverse_transform_sampling
% by ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Change the current folder to the folder of this mfile.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Plot the Rayleigh cumulative distribution function
% over the range 020 with a sigma of 5.
x = 0:20;
sigma = 5;
rayleighCDF = 1  exp(x.^2 / (2*sigma^2));
subplot(3,1,1);
plot(x, rayleighCDF, 'LineWidth', 3);
caption = sprintf('Rayleigh CDF with sigma = %.2f', sigma);
title(caption, 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Ask user for a number of random numbers to generate.
userPrompt = 'Enter an integer number of random numbers to generate';
caNumberOfRandoms = inputdlg(userPrompt, 'Enter an integer',1,
{'5000'});
numberOfRandoms = round(str2num(cell2mat(caNumberOfRandoms)));
% Check for a valid integer.
if isempty(numberOfRandoms)
% They didn't enter a number.
% They entered a character, symbols, or something else not
allowed.
numberOfRandoms = 5000;
message = sprintf('I said it had to be an integer.\nI will use %d
and continue.', numberOfRandoms);
uiwait(warndlg(message));
end
% Generate numberOfRandoms uniformly distributed random numbers.
uniformlyDistributedRandomNumbers = rand(numberOfRandoms, 1);
subplot(3,2,3);
bar(uniformlyDistributedRandomNumbers, 'BarWidth', 1);
xlim([0 numberOfRandoms]);
caption = sprintf('%d Uniformly Distributed Numbers',
numberOfRandoms);
title(caption, 'FontSize', fontSize);
xlabel('Element Number');
ylabel('Value');
%
% KEY PART, RIGHT HERE!!!!
% Invert the CDF of the Rayleigh function to get a function that can
% generate random numbers drawn from a Rayleigh distribution,
% given numbers drawn from a uniform distribution.
rayleighDistNumbers = sqrt(log(1
uniformlyDistributedRandomNumbers)*(2*sigma^2));
%
% Plot the Rayleigh distributed numbers.
subplot(3,2,4);
bar(rayleighDistNumbers, 'BarWidth', 1);
xlim([0 numberOfRandoms]);
caption = sprintf('%d Rayleigh Distributed Numbers', numberOfRandoms);
title(caption, 'FontSize', fontSize);
xlabel('Element Number');
ylabel('Value');
% Get histogram of uniformly distributed numbers.
[countsU, binsU] = hist(uniformlyDistributedRandomNumbers, 50);
% Plot the uniformly distributed numbers.
subplot(3,2,5);
bar(binsU, countsU, 'BarWidth', 1);
caption = sprintf('Histogram of %d Uniformly Distributed Numbers',
numberOfRandoms);
title(caption, 'FontSize', fontSize);
xlabel('Value');
ylabel('Count');
% Get histogram of Rayleigh distributed numbers.
% Observe that it's distribution is not flat like it is
% for the uniformly distributed numbers.
% It will take on the Rayleigh distribution shape.
[countsR, binsR] = hist(rayleighDistNumbers, 50);
subplot(3,2,6);
bar(binsR, countsR, 'BarWidth', 1);
caption = sprintf('Histogram of %d Rayleigh Distributed Numbers',
numberOfRandoms);
title(caption, 'FontSize', fontSize);
xlabel('Value');
ylabel('Count');
message = sprintf('Done with demo.\nNote the difference in the
histogram shapes\n(the bottom plots).\n\nDemo written by, and courtesy
of, ImageAnalyst');
uiwait(msgbox(message));
if numberOfRandoms > 300
promptMessage = sprintf('Note the lists of random numbers (the middle
plots) look biased higher\nbecause there are so many bars\nand the
bars are so squished together.\n\nHere is what it looks like when you
display fewer numbers');
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel',
'Continue');
if strcmp(button, 'Cancel')
return;
end
numberOfRandoms = 300;
subplot(3,2,3);
bar(uniformlyDistributedRandomNumbers(1 : numberOfRandoms),
'BarWidth', 1);
xlim([0 numberOfRandoms]);
caption = sprintf('%d Uniformly Distributed Numbers',
numberOfRandoms);
title(caption, 'FontSize', fontSize);
subplot(3,2,4);
bar(rayleighDistNumbers(1 : numberOfRandoms), 'BarWidth', 1);
xlim([0 numberOfRandoms]);
caption = sprintf('%d Rayleigh Distributed Numbers',
numberOfRandoms);
title(caption, 'FontSize', fontSize);
end
