Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Tracking imported data/curve to run calculations

Subject: Tracking imported data/curve to run calculations

From: Safa

Date: 13 May, 2011 01:19:06

Message: 1 of 6

I have obtained some data experimentally that basically has the distribution of a parameter I measured. It is not a straight line but a curve (2D), sometimes with one or more peaks. I have worked out a way to import this data into Matlab and I have .mat files with various lengths for the matrix. Now what I would like to do, is to be able to run calculations that basically tracks the experimentally generated curve. Initial thoughts were to have some code that selects random values along the entire length of the curve…which Matlab command/function would you suggest for this? is there some other command/function I could use in Matlab that could track the entire curve? I hope this query is not too generic in nature. Any hints on how to begin to tackle this would be really appreciated. Many thanks.

Subject: Tracking imported data/curve to run calculations

From: ImageAnalyst

Date: 13 May, 2011 02:02:56

Message: 2 of 6

Do you mean that you have a 1D array of Y values and you want to
simply take value elements from it at random "x" locations? If so,
then look at randperm().
m = randi(10, 1, 20)
randomIndexes = randperm(length(m))
randomlyPickedElements = m(randomIndexes)

Or, do you mean that you want random numbers as if they were drawn
from a probability distribution function that is the shape of your
curve? If so, you basically compute the CDF of your PDF and then
invert it and run uniformly distributed numbers through it. Check
this out:
http://en.wikipedia.org/wiki/Inverse_transform_sampling

Subject: Tracking imported data/curve to run calculations

From: Rune Allnor

Date: 13 May, 2011 04:50:35

Message: 3 of 6

On May 13, 3:19 am, "Safa " <enxs...@nottingham.ac.uk> wrote:
> I have obtained some data experimentally that basically has the distribution of a parameter I measured. It is not a straight line but a curve (2D), sometimes with one or more peaks. I have worked out a way to import this data into Matlab and I have .mat files with various lengths for the matrix.  Now what I would like to do, is to be able to run calculations that basically tracks the experimentally generated curve. Initial thoughts were to have some code that selects random values along the entire length of the curve…which Matlab command/function would you suggest for this? is there some other command/function I could use in Matlab that could track the entire curve?  I hope this query is not too generic in nature. Any hints on how to begin to tackle this would be really appreciated. Many thanks.

Why don't you work directly with the data themselves?

Rune

Subject: Tracking imported data/curve to run calculations

From: Safa

Date: 13 May, 2011 22:18:04

Message: 4 of 6

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 file http://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.

Subject: Tracking imported data/curve to run calculations

From: ImageAnalyst

Date: 13 May, 2011 23:52:00

Message: 5 of 6

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 m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Plot the Rayleigh cumulative distribution function
% over the range 0-20 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

Subject: Tracking imported data/curve to run calculations

From: Safa

Date: 14 May, 2011 00:41:05

Message: 6 of 6

Thanks very much for this demo. I just ran it and it works. Well written code and I will study it over the next couple of the days. I will also see if it is possible to tweak the code for normal/log distribution. Thanks again.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us