I'm working on MATLAB, where I have a vector which I need to split into two classes and then get a histogram of both resulting vectors (which have different sizes). The values represent height records so the interval is about 140-185.
How can I get a normalized histogram of both resulting vectors in different colors. I was able to get both normalized vectors in the same colour (which is indistiguible) and and also a histogram with different colours but not not normalized...
I hope you understand my question and will be able to help me.
Thanks in advance :)
Maybe this is what you need:
matrix = [155+10*randn(2000,1) 165+10*randn(2000,1)];
matrix(1:1100,1) = NaN;
matrix(1101:2000,2) = NaN; %// example data
[y x] = hist(matrix, 15); %// 15 is desired number of bins
y = bsxfun(#rdivide, y, sum(y)) / (x(2)-x(1)); %// normalize to area 1
bar(x,y) %// plots each column of y vs x. Automatically uses different colors
Related
Good day! I have two histograms and I can draw them overlaid on top of one another. But I need to find the sums in each of the columns and plot the summed histogram.
I cannot sum them up because the histogram is built using a function, and you cannot get the values in the column from it.
N = 50; % Total amount
x1 = randn(N,1); % Normally distributed numbers
x2 = rand(N,1)*2; % Normally distributed numbers and shifted
k = -5:0.5:5;
R1 = histogram(x1, k)
hold on
histogram(x2, k)
grid on
Maybe you need [x1,x2] for histogram, e.g.,
histogram([x1,x2],k)
If you have many variables like x1,x2, ..., xN, you can try
histogram(eval(sprintf("[%s]",strjoin(who("x*"),","))),k)
I need some help with MATLAB coding. I have two variables x=0:0.1:1 and y=0:0.1:1. I want to generate the meshgrid only for those points which satisfy the condition x+y<=1. Please help me.
[X,Y] = meshgrid(x,y) returns two matrixes, representing points from (0,0) to (1,1). The requirement to remove values where x + y >= 1 works well on a graph - basically just draw a diagonal line, creating a triangle. However, it doesn't work very well for matrices - who's ever heard of a triangle matrix?
What you can do is set the excluded values to some 'bad' value, and then ignore them. I chose to set them to NaN, because functions like surf won't plot nan's:
x = 0:.1:1;
y = 0:.1:1;
[X,Y] = surf(x,y);
X(X+Y>=1) = nan;
Y(X+Y>=1) = nan;
surf(X,Y,X.*Y)
I have a two columns of data. X = Model values of NOx concentrations and Y = Observations of NOx concentrations. Now, I want to scatter plot X, Y (markers varying with colors) as well as the colourbar which would show me the counts (i.e. number of data points in that range). X and Y are daily data for a year, i.e. 365 rows.
Please help me. Any help is greatly appreciated.
I have attached a sample image.
If I understand you correctly, the real problem is creating the color information, which is, creating a bivariate histogram. Luckily, MATLAB has a function, hist3, for that in the Statistics & Machine Learning Toolbox. The syntax is
[N,C] = hist3(X,nbins)
where X is a m-by-2 matrix containing the data, and nbins is a 1-by-2 vector containing the number of bins in each dimension. The return value N is a matrix of size nbins(1)-by-nbins(2), and contains the histogram data. C is a 1-by-2 cell array, containing the bin centers in both dimensions.
% Generate sample data
X = randn(10000, 1);
Y = X + rand(10000, 1);
% Generate histogram
[N,C] = hist3([X,Y], [100,100]);
% Plot
imagesc(C{1},C{2},N);
set(gca,'YDir','normal');
colormap(flipud(pink));
colorbar;
Result:
I'm trying to make a color plot in matlab using output data from another program. What I have are 3 vectors indicating the x-position, y-yposition (both in milliarcseconds, since this represents an image of the surroundings of a black hole), and value (which will be assigned a color) of every point in the desired image. I apparently can't use pcolor, because the values which indicate the color of each "pixel" are not in a matrix, and I don't know a way other than meshgrid to create a matrix out of the vectors, which didn't work due to the size of the vectors.
Thanks in advance for any help, I may not be able to reply immediately.
If we make no assumptions about the arrangement of the x,y coordinates (i.e. non-monotonic) and the sparsity of the data samples, the best way to get a nice image out of your vectors is to use TriScatteredInterp. Here is an example:
% samplesToGrid.m
function [vi,xi,yi] = samplesToGrid(x,y,v)
F = TriScatteredInterp(x,y,v);
[yi,xi] = ndgrid(min(y(:)):max(y(:)), min(x(:)):max(x(:)));
vi = F(xi,yi);
Here's an example of taking 500 "pixel" samples on a 100x100 grid and building a full image:
% exampleSparsePeakSamples.m
x = randi(100,[500 1]); y = randi(100,[500 1]);
v = exp(-(x-50).^2/50) .* exp(-(y-50).^2/50) + 1e-2*randn(size(x));
vi = samplesToGrid(x,y,v);
imagesc(vi); axis image
Gordon's answer will work if the coordinates are integer-valued, but the image will be spare.
You can assign your values to a matrix based on the x and y coordinates and then use imagesc (or a similar function).
% Assuming the X and Y coords start at 1
max_x = max(Xcoords);
max_y = max(Ycoords);
data = nan(max_y, max_x); % Note the order of y and x
indexes = sub2ind(size(data), max_y, max_x);
data(indexes) = Values;
imagesc(data); % note that NaN values will be colored with the minimum colormap value
So I'm still getting used to Matlab and am having a bit of trouble with plotting. I have a cell which contains a list of points in each row. I want to plot each row of points in a different colour on the same graph so I can compare them. The catch is that I need to make this work for an unknown number of points and rows (ie the number of points and rows can change each time I run the program).
So for example, I might have my cell array A:
A = {[0,0], [1,2], [3,4]; [0,0] [5,6], [9,2]}
and I want to plot the points in row 1 against their index (so a 3D graph) and then have the points in row 2 on the same graph in a different colour. The rows will always be the same length. (Each row will always have the same number of points). I've tried a few different for loops but just can't seem to get this right.
Any help in sending me in the right direction would be greatly appreciated!
The fact that the number of points and rows can change with each iteration should not pose much of a problem. I would suggest using the size function before your plot loops (size(A,1) and size(A,2)) to get the dimensions of the matrix.
Once you have the size of the matrix, loop through the dimensions and plot the lines on the same plot using holdon, and then finally just make the line color a function of the dimensions as you loop through so that you always have a different line color
You could just convert it to a matrix and plot it directly:
% Some dummy data - format a little different from your example
% to allow for different numbers of elements per row
A = {[0,0, 1,2, 3,4]; [0,0, 5,6]};
% Figure out how many columns we need in total
maxLen = max(cellfun(#length, A));
% Preallocate
Amat = NaN(size(A, 1), maxLen);
% Copy data
for n = 1:size(A, 1)
curA = A{n};
Amat(n, 1:length(curA)) = curA;
end
% Generate 1:N vector repeated the correct number of times (rows)
x = repmat(1:size(Amat, 2), size(Amat, 1), 1);
plot(x, Amat)
Edit: You mentioned a 3D graph at some point in your post. The above won't plot a 3D graph, so here's something that will:
% Generate Amat as above
% Then:
[X, Y] = meshgrid(1:size(Amat, 1), 1:size(Amat, 2));
surf(X, Y, Amat.'); % OR: plot3(X, Y, Amat.');
I'm not sure this is exactly what you want, but your question is slightly unclear on exactly what kind of graph you want out of this. If you just want coloured lines on your plot, you can use plot3 instead of surf, but IMHO surf will probably give you a clearer plot for this kind of data.