How to write a command in Matlab to check the deviation between one variable and others to be within +- %20? as if not in the range would print false if within the range print true
For example, I want to compare R1,R2,R3 with R and should be with the range of plus/minus %20:
R = 0.62
R1 = 0.638
R2 = 0.6337
R3 = 0.6354
Thanks
You want the absolute difference abs(var1-var2) to be smaller than some percentage. You compute the percentage by dividing a piece by the total abs(var1-var2)/var1. This value must be smaller than 0.2, so abs(var1-var2)/var1<0.2
Related
x=20;
LHS1=(sind(x)+cosd(x))^2
RHS1=1+2*sind(x)*cosd(x)
LHS1==RHS1
LHS2=(1-2*cosd(x)-3*(cosd(x))^2)/(sind(x))^2
RHS2=(1-3*cosd(x))/(1-cosd(x))
LHS2==RHS2
I am getting this answer as
LHS1 =
1.642787609686539
RHS1 =
1.642787609686539
ans =
1
LHS2 =
-30.163437477526365
RHS2 =
-30.163437477526383
ans =
0
That depends on how close you want the two values to be, to be considered "true". Can LHS2 and RHS2 be different by 0.1 and be considered equal? 0.001? 0.0000001? etc.
Decide on how close you want them to be, then check if the distance between the two are below some absolute amount.
threshold = 0.00001; % You decide this
difference = abs(LHS2 - RHS2); % Absolute distance between the two numbers
if (difference <= threshold)
% Do something
end
Note that you should not make the threshold too small, because computers have limits to how precise a decimal number can be (machine epsilon).
You may use ismembertol(x,y,tol) built-in function. In this case, x and y are your numbers while tol is your tolerance gap. You can make your tolerance really small to be able to sure about your equality.
For example,
x=1.000000001;
y=1;
tol=eps(1);
tol =
2.220446049250313e-16
ismembertol(x,y,tol)
ans =
logical
0
This means x and y are not equal.
I have a vector with real numbers only. The length of the vector is 5000 samples. I would like to find the indexes of the 10% of the highest samples (meaning 500 samples in total). Any idea of how to do that in Matlab?
first of all sort the sample in descending order and then select the top 10 percent:
[sortedVector, idxs] = sort(vector,'descend');
sampleRange = 1:floor(0.1*length(vector));
sample = sortedVector(sampleRange);
idxsSample = idxs(sampleRange);
This will do the job:
x = 1:100;
v = prctile(x,90);
res = find(x>v)
I put 90 in v because you were looking for the top 10%, that is, the 90-th percentile. The x above is just an example, you can replace it with your x.
I have 20 values x1,...x20. Each value is between 0 and 1, for example 0.22,0.23,0.25,...
x = rand(20,1);
x = sort(x);
Now I would like to choose one data point but not uniform at random. The data point with the lowest value should have the highest probability and the other values should have a probability proportional to the difference in function value to the lowest value.
For example, if the lowest function value is 0.22, a data point with a function value of 0.23 has a difference to the best value of 0.23 - 0.22 = 0.01 and should therefore have a probability similar to the 0.22 value. But a value of 0.3 has a difference of 0.3 - 0.22 = 0.08 and should therefore have a much smaller probability.
How can this be done?
I would leave this as a comment, but I unfortunately don't have the rep yet.
This looks interesting, and I have a few questions for you. (I will edit this answer to be an answer later.)
The data point with the lowest value should have the highest probability and the other values should have a probability proportional to the difference in function value to the lowest value.
Lets take an array of 20 items, and subtract the lowest number from the entire array. This leaves us with our smallest value (which you want to be the most probable) as 0. We need to define a function now, that goes over all of the points and integrates to 1.
I've done the following:
x = rand(20, 1);
x = sort(x);
xx = x - x(1);
I suppose at this point we can invert our answers so the lowest point is 1.
Px = 1 - xx; %For probabilities
TotalP = sum(Px);
Now we have everything we need, I think... So lets see what we can make.
P = Px/TotalP; %This will be our probability.
SanityCheck = sum(P); %Make sure that it sums up to 1.
Looks like that works, so lets make our cumulative sum array, and get an element.
PI = cumsum(P); %This will be the integral form of the probability function.
test = rand; %Create a test number so we can place it in the integral function
index = find(PI > test, 1); %This will return the first entry that is greater than our test value...
result = x(index); %And here's our value
I hope this is along what you were looking for. If not, please comment and I'll get back to you. :)
[edited to incorporate comments]
Can any one help me please to reverse the following normalization?
image_normalized = uint8(255*mat2gray(image));
This command normalizes a matrix values to 0-255. So, after doing some filtering (denoising), how can I transform the new filtered values from 0-255 to the original value space?
I do not exactly how mat2gray works, but I think that it perfomrs a "contrast expansion".
Ioutput[i,j] = ( Iinput[i,j]-min )* K / (max - min)
where min = min(Iinput(:)) and max = max(Iinput(:)) , and K is a constant.
And what you can do to reverse the normalization is to find K and save the max and min value of the input image.
On the other hand, if you are dealing with noise you should consider that the min or max value could be a corrupted pixel.
Below is some MATLAB code which is inside a loop that increments position every cycle. However after this code has run, the values in the vector F1 are all NaN. I print A to the screen and it outputs the expected values.
% 500 Hz Bands
total_energy = sum(F(1:(F_L*8)));
A = (sum(F(1:F_L))) /total_energy %% correct value printed to screen
F1(position) = (sum(F(1:F_L))) /total_energy;
Any help is appreciated
Assuming that F_L = position * interval, I suggest you use something like:
cumulative_energy_distribution = cumsum(abs(F).^2);
positions = 1:q;
F1 = cumulative_energy_distribution(positions * interval) ./ cumulative_energy_distribution(positions * 8 * interval);
sum-of-squares, which is the energy density (and also seen in Parseval's theorem) is monotonically increasing, so you don't need to worry about the energy going back down to zero.
In MATLAB, if you divide zero by zero, you get a NaN. Therefore, its always better to add an infinitesimal number to the denominator such that its addition doesn't change the final result, but it avoids division by zero. You can choose any small value as that infinitesimal number (such as 10^-10) but MATLAB already has a variable called eps.
eps(n) gives a distance to the next-largest number (whose precision is same as n) which could be represented in MATLAB. For example, if n=1, next double-precision number you could get to from 1 is 1+eps(1)=1+2.2204e-16. If n=10^10, then the next number is 10^10+1.9073e-06. eps is same as eps(1)=2.2204e-16. Adding eps doesn't change the output and avoids the situation of 0/0.