Change histogram across to left - matlab

I have plotted an histogram for the value 'Q' in my code below. I want the histogram to be from 0,1,2,3 format in the x-axis. At the movement I am not getting any value at 0. Ideally I want it to be shifted to the left and have the same plot starting from 0.
%Queue Buffer%
N = 1000;
Q = zeros(N,1);
Q2= 0;
s = 20;
for Ti = 2:N
Q(Ti) = Q2;
a = randi([0 32],1);
a1 = a - s;
a2 = Q(Ti) + a1;
Q2 = max(0,a2);
end
t = 0:N-1;
xout=hist(Q,9);
bar(xout, 'barwidth', 1, 'basevalue', 0);
https://dl-web.dropbox.com/get/Photos/qbuff.jpg?w=50ea6776

Either use
hist(Q,t);
Or
xout=hist(Q,t);
bar(t,xout);

bar([0:length(xout)-1], xout, 'barwidth', 1, 'basevalue', 1);

Related

Is there a possibility to have a quiver plot with vectors of same length?

I am using a quiver plot in MATLAB to simulate a velocity field. Now I would like the vectors produced by the quiver plot to be all the same length, so that they just indicate the vectors direction. The value of the velocity in each point should be illustrated by different colors then.
Is there a possibility to have quiver plotting vectors of same length?
That's my current code:
%defining parameters:
age = 900;
vis= 15;
turbulences = zeros(9,3);
a = 0.01;
spacing = 1000;
[x,y] = meshgrid(-100000:spacing:100000);%, 0:spacing:10000);
u = a;
v = 0;
n = 0;
for i = 1:4
turbulences(i,1) = -80000 + n;
turbulences(i,2) = 15000;
n = 15000 * i;
end
n = 0;
for i = 5:9
turbulences(i,1) = -15000 + n*5000;
turbulences(i,2) = 4000;
n = n+1;
end
for i = 1:4
turbulences(i,3) = -1000;
end
for i = 5:9
turbulences(i,3) = 800;
end
%compute velocities in x and y direction
for k = 1:9
xc = turbulences(k,1);
yc = turbulences(k,2);
r1 = ((x-xc).^2 + (y-yc).^2);
r2 = ((x-xc).^2 + (y+yc).^2);
u = u + turbulences(k,3)/2*pi * (((y-yc)./r1).*(1-exp(-(r1./(4*vis*age)))) - ((y+yc)./r2).*(1-exp(-(r2./(4*vis*age)))));
v = v - turbulences(k,3)/2*pi* (((x-xc)./r1).*(1-exp(-(r1./(4*vis*age)))) - ((x-xc)./r2).*(1-exp(-(r2./(4*vis*age)))));
end
quiver(x,y,u,v);
grid on;
Thank you for your help!
One way to do this would be to normalize each component of your vectors to +- 1 just to keep their direction.
un = u./abs(u); % normalized u
vn = v./abs(v); % normalized v
quiver(x, y, un, vn)

How to find Orientation of axis of contour in matlab?

I want to find Orientation, MajorAxisLengthand MinorAxisLength of contour which is plotted with below code.
clear
[x1 , x2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [1,3];
sigm = [2,0;0,2];
xx_size = length(mu);
tem_matrix = ones(size(x1));
x_mesh= cell(1,xx_size);
for i = 1 : xx_size
x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {x1,x2};
temp_mesh = [];
for i = 1 : xx_size
temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(x1));
figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');
% regionprops(z_plat,'Centroid','Orientation','MajorAxisLength','MinorAxisLength');
In my opinion, I may have to use regionprops command but I don't know how to do this. I want to find direction of axis of contour and plot something like this
How can I do this task? Thanks very much for your help
Rather than trying to process the graphical output of contour, I would instead recommend using contourc to compute the ContourMatrix and then use the x/y points to estimate the major and minor axes lengths as well as the orientation (for this I used this file exchange submission)
That would look something like the following. Note that I have modified the inputs to contourc as the first two inputs should be the vector form and not the output of meshgrid.
% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);
% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;
% Now loop through each contour
while start < size(contourmatrix, 2)
value = contourmatrix(1, start);
nPoints = contourmatrix(2, start);
contour_points = contourmatrix(:, start + (1:nPoints));
% Now fit an ellipse using the file exchange
ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));
% Increment the start pointer
start = start + nPoints + 1;
count = count + 1;
end
orientations = [ellipsedata.phi];
% 0 0 0
major_length = [ellipsedata.long_axis];
% 4.7175 3.3380 2.1539
minor_length = [ellipsedata.short_axis];
% 4.7172 3.3378 2.1532
As you can see, the contours are actually basically circles and therefore the orientation is zero and the major and minor axis lengths are almost equal. The reason that they look like ellipses in your post is because your x and y axes are scaled differently. To fix this, you can call axis equal
figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');
axis equal
Thank you #Suever. It help me to do my idea.
I add some line to code:
clear
[X1 , X2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [-1,0];
a = [3,2;1,4];
a = a * a';
sigm = a;
xx_size = length(mu);
tem_matrix = ones(size(X1));
x_mesh= cell(1,xx_size);
for i = 1 : xx_size
x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {X1,X2};
temp_mesh = [];
for i = 1 : xx_size
temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(X1));
figure;contour(X1, X2, z_plat,3, 'LineWidth', 2,'color','m');
hold on;
% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);
% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;
% Now loop through each contour
while start < size(contourmatrix, 2)
value = contourmatrix(1, start);
nPoints = contourmatrix(2, start);
contour_points = contourmatrix(:, start + (1:nPoints));
% Now fit an ellipse using the file exchange
ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));
% Increment the start pointer
start = start + nPoints + 1;
count = count + 1;
end
orientations = [ellipsedata.phi];
major_length = [ellipsedata.long_axis];
minor_length = [ellipsedata.short_axis];
tet = orientations(1);
x1 = mu(1);
y1 = mu(2);
a = sin(tet) * sqrt(major_length(1));
b = cos(tet) * sqrt(major_length(1));
x2 = x1 + a;
y2 = y1 + b;
line([x1, x2], [y1, y2],'linewidth',2);
tet = ( pi/2 + orientations(1) );
a = sin(tet) * sqrt(minor_length(1));
b = cos(tet) * sqrt(minor_length(1));
x2 = x1 + a;
y2 = y1 + b;
line([x1, x2], [y1, y2],'linewidth',2);

Random Walk, simplifying the code

I am currently practicing modelling "random walks" and population modelling. I have a working code that works for a 1D random walk, in the future I'd like to add extra dimensions and I think the code I have at the moment would make it more difficult. I need to plot the first two positions before I can start the for loop. Ideally I would like to get rid of that issue and have it start from the first step.
numjumps = 20; %number of steps
R = 0.5; %probability of step to right
more off
prev_position = [0 0]; %first position
x = rand(1); %generate random number 0-1
if x >= R;
step = 1; %take a step to the right
elseif x < R
step = -1; %take a step to the left
end
position = [1 prev_position(2) + step];
close all;
figure;
hold on;
plot([prev_position(1) position(1)], [prev_position(2) position(2)]);
axis([0 numjumps -5 5])
for i = (2:numjumps);
prev_position(i) = position(i-1);
x = rand(1); %generate random number 0-1
if x >= R;
step = 1; %take a step to the right
elseif x < R
step = -1; %take a step to the left
end
position(i) = [prev_position(i) + step]; %add step to position
plot(position);
axis([0 numjumps -5 5])
end
total = sum(position);
mean_position = total/numjumps;
disp(mean_position);
Any help would be greatly appreciated.
You shouldn't use a for loop, just vectorize your code:
numjumps = 20; %number of steps
R = 0.5; %probability of step to right
x = rand (numjumps, 1);
step = 2 * (x >= R) - 1;
position = [0; cumsum(step)];
plot (position)
now it's easy to make it 2D:
numjumps = 200; %number of steps
R = 0.5; %probability of step to right
x = rand (numjumps, 2);
step = 2 * (x >= R) - 1;
position = [0, 0; cumsum(step)];
plot (position(:, 1), position (:, 2))
gives

Sequence of dots in matlab and psychotoolbox

How would i display one by one dots that are in a 3x3 matrix such as in the code below?
I would like to have dot1 appears in position [x1,y1] of the grid for a time t1, then dot2 to appears in position [x2,y2] of the grid for a time t2. Only one dot is being shown at each time.
Thanks for help
%grid
dim = 1
[x, y] = meshgrid(-dim:1:dim, -dim:1:dim);
pixelScale = screenYpixels / (dim * 2 + 2);
x = x .* pixelScale;
y = y .* pixelScale;
% Calculate the number of dots
numDots = numel(x);
% Make the matrix of positions for the dots.
dotPositionMatrix = [reshape(x, 1, numDots); reshape(y, 1, numDots)];
% We can define a center for the dot coordinates to be relaitive to.
dotCenter = [xCenter yCenter];
dotColors = [1 0 0];
dotSizes = 20;
Screen('DrawDots', window, dotPositionMatrix,...
dotSizes, dotColors, dotCenter, 2);
I think you want something like this?
%positions of each successive dots:
x_vec = [1,2,3,1,2,3,1,2,3];
y_vec = [1,1,1,2,2,2,3,3,3];
%wait times in sec for each dot:
wait_times = [1,1,2,1,1,2,1,1,2]
dotColor = [1 0 0];
dotSize = 400;
num_dots = length(x_vec);
for i = 1:num_dots
scatter(x_vec(i),y_vec(i),dotSize,dotColor,'filled');
xlim([0,max(x_vec)])
ylim([0,max(y_vec)])
pause(wait_times(i));
end

Prewitt Filter implementation Matlab

I'm trying to implement the Prewitt Filter in Matlab. I know that Matlab has already this kind of filter but I need to code it myself. Below is my code, the only problem is that at the end of the filtering I get a bright image instead of seeing the edges.
I'm implementing the filter using the separability property of the Prewitt Filter. Any ideas? I will appreciate very much your help.
%% 3x3 Prewitt Filter
close all
imageIn = imread('images/Bikesgray.jpg');
imageGx = zeros(size(imageIn));
imageGy = zeros(size(imageIn));
imageOut = zeros(size(imageIn));
ny = size(imageIn, 1);
nx = size(imageIn, 2);
average = 3;
imshow(imageIn);
u = [];
v = [];
tic
%Compute Gx
%For every row use the mask (-1 0 1)
for i = 1:ny
u = imageIn(i,:);
v = zeros(1, nx);
for k = 2:nx-1
v(k) = (uint32(-1*u(k-1))+uint32(0*u(k))+uint32(u(k+1)));
end
v(1) = (uint32(-1*u(2))+uint32(0*u(1))+uint32(u(2)));
v(nx) = (uint32(-1*u(nx-1))+uint32(0*u(nx))+uint32(u(nx-1)));
imageGx(i,:) = v;
end
%For every column use the mask (1 1 1)
for j = 1:nx
u = imageGx(:,j);
v = zeros(ny, 1);
for k = 2:ny-1
v(k) = (uint32(u(k-1))+uint32(u(k))+uint32(u(k+1)));
end
v(1) = (uint32(u(2))+uint32(u(1))+uint32(u(2)));
v(ny) = (uint32(u(ny-1))+uint32(u(ny))+uint32(u(ny-1)));
imageGx(:,j) = v;
end
%Compute Gy
%For every row use the mask (1 1 1)
for i = 1:ny
u = imageIn(i,:);
v = zeros(1, nx);
for k = 2:nx-1
v(k) = (uint32(u(k-1))+uint32(u(k))+uint32(u(k+1)));
end
v(1) = (uint32(u(2))+uint32(u(1))+uint32(u(2)));
v(nx) = (uint32(u(nx-1))+uint32(u(nx))+uint32(u(nx-1)));
imageGy(i,:) = v;
end
%For every column use the mask (1 0 -1)
for j = 1:nx
u = imageGy(:,j);
v = zeros(ny, 1);
for k = 2:ny-1
v(k) = (uint32(u(k-1))+uint32(0*u(k))+uint32(-1*u(k+1)));
end
v(1) = (uint32(u(2))+uint32(0*u(1))+uint32(-1*u(2)));
v(ny) = (uint32(u(ny-1))+uint32(0*u(ny))+uint32(-1*u(ny-1)));
imageGy(:,j) = v;
end
toc
figure
imshow(imageGx, [0 255]);
figure
imshow(imageGy, [0 255]);
%Compute the magnitude G = sqrt(Gx^2 + Gy^2);
imageOut(:,:) = sqrt(imageGx(:,:).^2 + imageGy(:,:).^2);
figure
imshow(imageOut, [0 255]);
It's too bad you didn't use convn (convolution), since the weighted sum just screams it.
In a nutshell you produce Gx,Gy by using convn on the image matrix, using the appropriate kernels, as described in wikipedia
The solution was really obvious but took me some time to figure it out.
All I did is change the uint32 to int32 and be sure to perform the operations (e.g. multiplying by -1) after changing the values from uint32 to int32.