I am just starting to learn Matlab.
Case:
From 3 elements, let's say 1,2, and 3. I want to sample 2 elements randomly. I want to simulate it 100 times to see the probability of the outcomes pair.
How can I plot the result on histogram that I can visualize the frequency of each pair. So far, I can do the sampling :
for i=1:100
datasample(1:3,2,'Replace',true)
end
So possible outcome is (1,1),(1,2),(2,1),(2,3), etc.
How can I plot the frequency of the outcome using histogram?
Thanks in advance
n = 100;
% generate data random
arr = zeros(n, 2);
for i = 1:n
arr(i, :) = randi([1,3],1,2);
end
% frequency
[ii, jj, kk] = unique(arr, 'rows', 'stable');
f = histc(kk, 1:numel(jj));
result = [ii f];
% plot
cuts = strcat(num2str(result(:,1)), '-',num2str(result(:,2)));
bar(result(:,3))
grid on
xlabel('combination')
ylabel('frequency')
set(gca,'xticklabel',{cuts});
set(gca,'XTickLabelRotation',45);
Related
I'm trying to simulate some random variables Y such that P(Y=1)=P(y=-1)=0.5, and X_n = sum of Y_i (i from 1 to n). I want to use matlab to simulate X_n and plot it versus different n's, where n = 1,2,3,...100. Here is my matlab code:
N = 100;
for M = 1:N
y_i = randi([-1 1], M, 1);
X_n = sum(y_i);
end
plot(M, X_n)
But my plot looks like this, can someone help me fix it? Is there something wrong with my code? Thank you.
Seems like somebody provided you with the right answer already but let me explain and how i would go about it. The only thing you're doing wrong is about the indexing. Try this.
N = 100; % sets your maximum
for M = 1:N % loops from 1 - N
y_i = randi([-1 1], M, 1); % your formula
X(M) = sum(y_i); % stores your data in vectors with increasing index from 1 - 100
end
index = 1:N % generates a vector 1-100 to serve as indexes
plot(index, X) % plots each point of X a corresponding index
I have a matrix M(mx2). The first column is my bins and the second one is the frequency associated with each bin. I want to fit a smooth curve to this histogram in matlab, but most of what I have tried (like kdensity) needs the real distribution of data, which I don't have them.
Is there any functions that can take the bins and their frequency and give me a smooth curve of bin-freq. ?
Here's a hack that should work for you: generate a sample from your histogram, then run ksdensity on the sample.
rng(42) % seed RNG to make reproducible
% make example histogram
N = 1e3;
bins = -5:5;
counts = round(rand(size(bins))*N);
M = [bins' counts'];
figure
hold on
bar(M(:,1), M(:,2));
% draw a sample from it
sampleCell = arrayfun( #(i) repmat(M(i,1), M(i,2), 1), 1:size(M,1), 'uniformoutput', false )';
sample = cat(1, sampleCell{:});
[f, x] = ksdensity(sample);
plot(x, f*sum(M(:,2)));
How can I generate integer random number within [a,b] with below distribution in MATLAB:
p(x)= x^(-a)
I want the distribution to be normalized.
For continuous distributions: Generate random values given a PDF
For discrete distributions, as later it was specified in the OP:
The same rationale can be used as for continuous distributions: inverse transform sampling.
So from mathematical point of view there is no difference, the Matlab implementation however is different. Here is a simple solution with your distribution function:
% for reproducibility
rng(333)
% OPTIONS
% interval endpoints
a = 4;
b = 20;
% number of required random draws
n = 1e4;
% CALCULATION
x = a:b;
% normalization constant
nc = sum(x.^(-a));
% if a and b are finite it is more convinient to have the pdf and cdf as vectors
pmf = 1/nc*x.^(-a);
% create cdf
cdf = cumsum(pmf);
% generate uniformly distributed random numbers from [0,1]
r = rand(n,1);
% use the cdf to get the x value to rs
R = nan(n,1);
for ii = 1:n
rr = r(ii);
if rr == 1
R(ii) = b;
else
idx = sum(cdf < rr) + 1;
R(ii) = x(idx);
end
end
%PLOT
% verfication plot
f = hist(R,x);
bar(x,f/sum(f))
hold on
plot(x, pmf, 'xr', 'Linewidth', 1.2)
xlabel('x')
ylabel('Probability mass')
legend('histogram of random values', 'analytical pdf')
Notes:
the code is general, just replace the pmf with your function;
it is strange that the same parameter a appears in the distribution function and in the interval too.
I have a function that plots the magnitude of an fft function from a signal.
For every iteration I want to determine the x-value of the two peaks below 2000. I thought this was relatively simple using the function findpeaks however it has not given me the correct output.
I do not intend to plot the ouput, but just for illustration purposes here is a plot. I only want to know the peaks for the data below 2000 (the first set of peaks)
Example of one iteration:
Here is a bit of my code. B is a vector containing the starting indices for every segment of data that needs to be analysed.
function [number] = fourir_(data,sampling_rate)
%Finds the approximate starting index of every peak segment
%B is a vector containing the indeces
[A,B] = findpeaks(double(abs(data) > 0.6), 'MinPeakDistance', 2500);
Fs = sampling_rate;
t = 0:1/Fs:0.25;
C = zeros(size(B),2)
for i = 1:numel(B)
new_data = data(B(i):(B(i)+200))
y = double(new_data)/max(abs(new_data));
n = length(y);
p = abs(fft(y));
f = (0:n-1)*(Fs/n);
end
Example data: https://www.dropbox.com/s/zxypn3axoqwo2g0/signal%20%281%29.mat?dl=0
Here is your answer, this is exactly what #Ed Smith suggested in his first comment. You can just add a threshold in order to distinguish the major peak.
%Finds the approximate starting index of every peak segment
%B is a vector containing the indeces
[A,B] = findpeaks(double(abs(data) > 0.6), 'MinPeakDistance', 2500);
Fs = sampling_rate;
t = 0:1/Fs:0.25;
C = zeros(size(B),2)
for i = 1:numel(B)
new_data = data(B(i):(B(i)+200))
y = double(new_data)/max(abs(new_data));
n = length(y);
p = abs(fft(y));
f = (0:n-1)*(Fs/n);
p1 = p(1:round(length(p)/2));
p1(p1<10) = 0; %add a threshold
[~,ind] = findpeaks(p1); %index of where are the peaks
C(i,:) = f(ind);
hold on
plot(f,p,'b',C(i,:),p(ind),'ro')
end
The following may help, which seems to get the peaks from one fft of your signal data,
clear all
close all
%load sample data from https://www.dropbox.com/s/zxypn3axoqwo2g0/signal%20%281%29.mat?dl=0
load('./signal (1).mat')
%get an FFT and take half
p = abs(fft(signal));
p = p(1:length(p)/2);
%find peaks and plot
[pk, loc] = findpeaks(p,'MINPEAKHEIGHT',100,'MINPEAKDISTANCE',100);
plot(p,'k-')
hold all
plot(loc, pk, 'rx')
which looks like,
Where some of the peaks are isolated...
I have two classes(normally distributed), C1 and C2, each defined by their mean and standard deviation. I want to be able to visualize the pdf plot of a normal distributions and the classification boundary between the two. Currently I have the code to plot the distributions but I'm not sure how to go about plotting the decision boundary. Any ideas would be appreciated. I have included a sample of what I want to plot. 1
Many thanks!
This is what I came up with:
% Generate some example data
mu1 = -0.5; sigma1 = 0.7; mu2 = 0.8; sigma2 = 0.5;
x = linspace(-8, 8, 500);
y1 = normpdf(x, mu1, sigma1);
y2 = normpdf(x, mu2, sigma2);
% Plot it
figure; plot(x, [y1; y2])
hold on
% Detect intersection between curves; choose threshold so you get the whole
% intersection (0.0001 should do unless your sigmas are very large)
ind = y1 .* y2 > 0.0001;
% Find the minimum values in range
minVals = min([y1(ind); y2(ind)]);
if ~isempty(minVals)
area(x(ind), minVals)
end
I don't know if this is the best way to do what you want, but it seems to work.