how to save the `Z values` of the data points into a variable for comparing it to a certain z value in matlab? - matlab

The Z value is calculated in the function using the following code:
[marg1,marg2] = meshgrid(marginalLogLikelihood_grid{1},marginalLogLikelihood_grid{2});
[xg,yg] = meshgrid(marginalCDFValues_grid{1},marginalCDFValues_grid{2});
inputMatrix = [reshape(xg,numel(xg),1) reshape(yg,numel(yg),1)];
clear xg yg;
copulaLogLikelihoodVals = gmmCopulaPDF(inputMatrix,gmmObject,xmesh_inverse_space);
Z = reshape(copulaLogLikelihoodVals,size(marg1,1),size(marg1,2));
Z = Z+marg1+marg2;
Z = exp(Z);
I want to use this Z value to compare it with a particular value of Z = 4e-5 and eliminate all the points that lie outside the contour line with Z = 4e-5.
I've written the following code to generate the contour and the scatter plot
plot(givenData(:,1),givenData(:,2),'b.','MarkerSize',3);hold
contour(xgrid,ygrid,Z,[4e-5, 4e-5],'EdgeColor',[1 0 0],'ShowText','on','LineWidth',2);
I want to eliminate all the points lying outside the red contour in the image below:
current image
Any help is appreciated. Thank you.

Use something like this:
lmt = 4e-5;
Z(Z>lmt) = NaN;
You can also modify the arrays with indices if you want to remove them altogether
lmt = 4e-5;
idx = Z>lmt;
xgrid(idx) = [];
ygrid(idx) = [];
Z(idx) = [];
Note that it's not clear whether you want to remove points greater than or less than 4e-5, so modify the inequality as you need.

Related

How to make a set on plot in Matlab like on the photo - Perceptron

I have a problem to make a set and show like on this photo in attachment.
Where:
Points must to be random in the range of:
Principle to set:
This is first part of exercise about perceptron. Without this I can't make other parts.
There is my code:
clc;
close all;
clear all;
I=400;
x1=-1+rand(I/2,1)+1;
X = [0+rand(I,1)*(2*pi) [-1+rand(I/2,1)+1;
(-1+rand(I/2,1)+1)] ]
Y = [ones((I/2),1)*sin(-1); ones(I/2,1)];
a = X(1:I/2,1);
b = X(1:I/2,2);
c = X(I/2:I,1);
d = X(I/2:I,2);
plot( a, b, 'bx');
hold on;
plot( c, d, 'go');
So you already figured out how to generate the random coordinates within the area of the plot:
N = 400;
x1 = rand(N,1)*(2*pi);
x2 = rand(N,1)*2-1;
Next, you want to find the subset of points that satisfy the equation (this is the set for which y==-1):
I = abs(sin(x1)) > abs(x2);
I is a logical array, with true values where the condition is satisfied. You can use I to index into another array. For example, You can create the vector y like this:
y = ones(N,1);
y(I) = -1;
But you don't really need y to create the plot. You were already plotting two subsets, simply make the subsets using I as index, instead of 1:N/2:
plot(x1(I),x2(I),'bx');
hold on
plot(x1(~I),x2(~I),'go');
The result is a plot exactly like in the question, except with x and o markers instead of . markers.

MATLAB: Finding minimum within a selected histogram region

So I created an image histogram in Matlab, and the user is able to use imrect to select a desired region.
I would like to be able to find the minimum y-value from the region that the user selects.
Here's what I have so far:
handles.pet_hist_rect = imrect(gca);
[xmin ymin width height] = getPosition(handles.pet_hist_rect);
xdata = get(findobj(gcf,'type','patch'),'xdata');
ydata = get(findobj(gcf,'type','patch'),'ydata');
I'm not sure how to extract the minimum y-value (from ydata) in the range [xmin, xmin+width]
Thanks in advance!
I think your code doesn't run in the first place, cause you are trying to assign the output of getPosition (a 1x4 array) to the single entries of another array, which doesn't work. After correcting this to
position = getPosition(handles.pet_hist_rect);
you can now access xmin as position(1), ymin as position(2) and so on.
Now, ymin = position(2) is already what you are asking for (minimum of y), but I'm not sure, I'm getting you right here. There is no need to query the graphics properties. If this is not what you are looking for, you are going to have to rephrase the question a little bit.
Edit: The following is super crude, should work, does not work however, if any of the histogram counts are zero!
close all;
hist(rand(1000, 1));
handles.pet_hist_rect = imrect(gca);
position = getPosition(handles.pet_hist_rect);
xdata = get(findobj(gcf,'type','patch'),'xdata');
ydata = get(findobj(gcf,'type','patch'),'ydata');
x = xdata{1};
x(x < position(1))=0;
x(x > position(1) + position(3))=0;
x(x>0) = 1;
y = ydata{1}(logical(x));
y(y==0) = NaN;
m = min(y);

Add space between plots for the plotmatrix function

Is there a way to add some space between the plots for the plotmatrix function? (I would like to label every x and y axis)
You can do it by using a specific output argument in the call to plotmatrix. You can then retrieve the position of each individual axes and modify it (making it smaller).
Example:
clc
clear
rng default
X = randn(50,3);
Y = reshape(1:150,50,3);
%// Use this output argument
[~,AX,~,~,~] = plotmatrix(X,Y);
%// Fetch all the positions and alter them (make axes smaller)
AllPos = get(AX(:),'Position');
AllPos = vertcat(AllPos{:});
NewPos = [AllPos(:,1)+.05 AllPos(:,2)+.05 AllPos(:,3)-.1 AllPos(:,4)-.1]
%// Update the plot
for k = 1:numel(AX)
axes(AX(k))
set(AX(k),'Position',NewPos(k,:))
xlabel(sprintf('Axes %i',k))
end
Outputs the following:
In contrast to the original plot:

Can't I use graphshortestpath function at here?

I want to calculate Diameter of graph, which means greatest distance between any two vertices of G.
cm is connectivity matrix of graph, and diameter of graph should be in variable a.
But MATLAB gave me some error message 'Input argument should be a sparse array.'
Can't I use graphshortestpath function to calculate diameter? Then what should I do instead?
cm = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
bg = biograph(cm);
a = 1;
for i = 1:4
for j = (i+1):5
[dist,path,pred] = graphshortestpath(bg,i,j)
if a<=dist
a = dist
end
end
end
I haven't tested this (I don't have MATLAB here), but how about making cm sparse, and use that as input to graphshortestpath?
According to the documentation, "[The first argument must be an] N-by-N sparse matrix that represents a graph. Nonzero entries in matrix G represent the weights of the edges." Thus, you should not use the biograph as input.
Check our the first example in the documentation, it explains it very well!
cm_full = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
cm = sparse(cm_full);
bg = biograph(cm);
a = 1;
for i = 1:4
for j = (i+1):5
[dist,path,pred] = graphshortestpath(cm,i,j)
if a<=dist
a = dist
end
end
end
end

Wrong with function 'plot', maybe

I want to calculate Nodal clustering coefficient distribution with a connectivity matrix
But when I operate this code, it return nothing. What is the problem?
Can't I use function plot like that?
cm = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
bg = biograph(cm);
for i = 1:5
intNodes = getrelatives(bg.nodes(i));
num = numel(intNodes);
plot(i,num);
end
Each call to plot will erase the previously plotted data, unless you write
hold on
before the loop (or unless you edit the axes properties).
cm = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
bg = biograph(cm);
figure %# create new figure
hold on %# create axes, make sure that plots get added instead of replaced
for i = 1:5
intNodes = getrelatives(bg.nodes(i));
num = numel(intNodes);
plot(i,num);
end
Your code is probably going to run better if you first gather all the data points, and only plot them in the end. Here is how this can be done:
cm = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
bg = biograph(cm);
plotData = NaN(5,1)
for i = 1:5
intNodes = getrelatives(bg.nodes(i));
num = numel(intNodes);
plotData(i)=num
end
plot(1:5,plotData)