Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this function
Y=fft(y); %fourier transformation
n=size(y,2)/2;
AS=abs(Y)/n; %absolute value
set(figure, 'Position', [0 0 500 300]) %left,bottom,width,height
freq=(0:79)/(2*n*dt);
stem(freq,AS(1:80));
I don't want circles around abscissa axis. I want them only on top of graph.
You can skip plotting points where AS would be equal to 0. Simply set these values to NaN, then plot your graph:
Y=fft(y); %fourier transformation
n=size(y,2)/2;
AS=abs(Y)/n; %absolute value
set(figure, 'Position', [0 0 500 300]) %left,bottom,width,height
freq=(0:79)/(2*n*dt);
%// NEW
ASval = AS(1:80);
ASval(ASval == 0) = NaN;
stem(freq,ASval);
What will happen is that any points that are exactly 0 will not be plotted due to the insertion of NaN. Any values that are non-zero will be plotted by stem normally.
In general, due to floating point precision, looking for elements that are exactly 0 may not bode well. As such, it is good to check to see if values are within a specified threshold, and if they are, set these values to NaN. Because your data is strictly positive, there isn't a need to check for values that are approaching from the negative side of the horizontal axis. As noted in your comments, you used 0.15. Therefore, you would simply do this instead of what I had above:
%// NEW
ASval = AS(1:80);
ASval(ASval < 0.15) = NaN;
stem(freq,ASval);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have 1D vector. For example: y=[0.2 0.9 1.0 1.0]. I can plot it with plot(y) to get a graph of y(x) where x values are just indices [1, 2, 3, 4].
Now, instead of x values being just indices, I want to map them to [0,1] range: x = linspace(0,1,length(y)). I get: x=[0 0.3333 0.6667 1.000].
I can now make a graph with plot(x,y):
Now, however, I want an inverse graph, so I make a plot with plot(y,x):
I want to be able to now use plot(x) to get the same shape as above. However, if I use plot(x), as expected, I just get a straight line.
How to transform x in such a way that plot(x) will give the same shape as plot(y,x)?
Upd.: If I try just 1./x:
I have managed to find a solution, so for anybody who also need its:
x = linspace(0,1,length(y));
% not needed in this toy example, but can be required for a bigger vector:
[y_unique, idx] = unique(y);
inv_y = interp1(y_unique,x(idx),x);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to MATLAB and I want to create a random n*n matrix containing just -1 OR 1 as values.
any help ?
I would use randi
% Generate random array of 0s and 1s, *2 and -1 to give random values -1 or +1
m = randi([0,1], n)*2-1
See also: introductory docs on random integers.
I typically use randi to generate the indexes of the numbers I am interested in. E.g. in your case you are interested in the numbers
a= [-1,1];
Thus we use
b = randi(length(a),2,2); %Generate matrix of size 2x2
to generate a random set of indexes. Finally we simply convert the indexes to the numbers of interest.
c = a(b); %Now a 2x2 matrix of -1, 1 numbers
A=rand(n);
thres=rand(1); % or whatever percentage
A=A>thres; % 1 and 0
A(A==0)=-1; % makes 0 -1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using the meshgrid function in Matlab (v. R2011b) to plot uneven thicknesses data (z) onto a long (x) and lat (y) grid before interpolating and taking an average.
(i) I have defined the spacing for meshgrid but do not know if this is evenly spaced, as the values are different in each dimension (x = 0.00025, and y = 0.0005)? What are the units for this spacing? If this is not even, are there any suggestions of what the spacing should be to create an even grid?
(ii) If this is an even grid, can anyone briefly explain how this is possible given that there are different numbers in the x and y direction?
[xi, yi] = meshgrid(25.32473:0.00025:25.426483, 36.363799:0.0005:36.49821)
There is a definition of uniform grid, where the size of the grid remains the same throughout. meshgrid will always produce a uniform grid. The x-direction and y-direction grid increments can differ in this case. You should not worry about the the increments as such if it solves your use-case.
The unit of increment is same as the unit of axis. For example, if the unit of the axis is meters, then the unit of increment would be 0.005 meters, if your increment size if 0.005.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have trouble with the probability density function (p.d.f) of a log-normal distribution. I really need your help. As defined by wikipedia:
http://en.wikipedia.org/wiki/Log-normal_distribution#Probability_density_function
The probability density function of a log-normal distribution is:
My problem is, how to define x variable in MATLAB? Thanks for your help!
If you have the Stats toolbox, you can just use lognpdf:
y = lognpdf(x,mu,sigma);
Though this is a very simple function - fully vectorized, it's effectively just:
y = exp(-0.5*((log(x)-mu)./sigma).^2)./(x.*sqrt(2*pi).*sigma);
But you may want to check that x > 0 and sig > 0. To create this plot on the Wikipedia article that you cited, you can do:
mu = 0;
sigma = [1;0.5;0.25];
x = 0:0.01:3;
y = lognpdf([x;x;x],mu,sigma(:,ones(1,length(x))));
figure; plot(x,y);
When your question asks about defining x, maybe you're actually looking for log-normally distributed random variables, i.e., you want to sample randomly from the log-normal PDF/distribution? In that case you can use lognrnd:
r = lognrnd(mu,sigma);
I'm confused, like you can do this in a one-liner,
fun = #(x,mu,sigma) (1./(x*sigma*sqrt(2*pi))).*exp( -(power((log(x)-mu),2))/(2*power(sigma,2)))
x is any value that satisfies x > 0, the pdf tells you via Wikipedia
In probability theory, a probability density function (pdf), or
density of a continuous random variable, is a function that describes
the relative likelihood for this random variable to take on a given
value.
So any value x given to the log-normal pdf tells you tel relative likelihood that a random variable could be that value.
Consider this toy example:
mu = 1;
sigma = 10;
x = logspace(-2,0,10);
plot( x, fun(x,1,10) )
From this plot as x gets closer to zero, it's relative likelihood of actually taking on that value increases. DISCLAIMER I just threw that function together, it needs to be checked for accuracy, the preceding was for illustration only.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a task to implement a 3x3 x-derivative image filter that makes use of central difference and performs at the same time a Gaussian smoothing in the y direction.
I have a formula for that task in the x-direction (h=1), and I am not sure whether I understand it correctly:
(f(x+h;y)-f(x-h;y)) / 2*h
Relative from my current pixel (x), I take the pixel value +1 ahead of my current pixel and subtract the value from the pixel on the position -1 behind my current pixel. Is this value divided by 2 then more or less my first order derivation in the x direction? Isn't my actual current pixel value used at all?
Traditionally for images, the center isn't used for a derivative filter. The reasoning can be found as follows.
The purpose of most image processing filters is to find things in the original image, and to identify them. Thus, a good kernel (*) will be centered at the same location as the change.
The derivative should be zero meaned. If you add all of the pixels from the kernel, they should add up to zero.
Given these two points, and especially the first one, it is evident that the kernel should have an odd number of elements, and the center should be 0. Essentially, if the kernel is odd, then it will tend to preserve the original edge.
Taking a 1 dimensional example, and apply the formula you provided, trimming the edges (Forcing them to 0), will result as follows:
[0 1 3 2 10 12 8 11];
[0 -2 -1 -7 -10 2 1 0 ];
Note that the two highest magnitude values are right at the peak. Try playing with other kernels, including some that don't have the center value be 0, and see what results.
(*) Kernel represents the function you are performing. In the case you have provided, the kernel is [1 0 -1]