How do I find the percentage of a number of values within a decimal range?
4 views (last 30 days)
Show older comments
I would like to know the percentage of values which lie within a certain range e.g 0.500000 and 0.00000. where 0.000000=100% and 0.500000=0%. i've tried using:
percent=sum(x <= 0.500000 & x >= 0.000000) / numel(x) %number of values between 0.000000 and 0.500000 divided by total number. x was from other process before this codes. its a decimal between those range.
set(handles.percentbox,'string',percent);
and the output is always 1. how to fix this?
2 Comments
Jan
on 14 Jul 2017
Edited: Jan
on 14 Jul 2017
The question ist not clear. Does
sum(x <= 0.500000 & x >= 0.000000) / numel(x)
give you the wanted result? Then is it only a problem of the display in the GUI?
Has "percent" been defined with integer type before, such that the assigned value is rounded?
I cannot imagine what "0.0=100% and 0.5=0%" means. If you are looking for the relation of the values inside a range, it is not meaningful to assigne percentage values to the limits.
Cam Salzberger
on 14 Jul 2017
Frequently in this kind of situation, it helps to divide up the problem. What do you get when you just do:
x = [0 0.25 0.5 0.75 1];
percent = nnz(x <= 0.5 & x >= 0) / numel(x);
disp(percent)
or look at your "percent" variable in the Variable Browser. This will tell you if you are calculating "percent" in the way that you want to. I'm really not sure what you mean by 0.000000=100% and 0.500000=0%.
Once you determine that you are calculating "percent" correctly, you can then try to put it into the uicontrol. As Walter said, you'll want to multiply by 100 if you want a true percentage. As J Smith said, you'll want to convert the number to a string before setting it to the String property of a uicontrol.
If that's not working, try opening the GUI in GUIDE, and hard-coding the value in there. Does that work as expected? You can also try specifying precision with more control using either:
num2str(percent,'%.2f')
or
sprintf('%.2f',percent)
So what are you getting for disp(percent)?
Answers (3)
ES
on 12 Jul 2017
This looks fine to me except the second line.
set(handles.percentbox,'string', num2str(percent));
what is your x?
3 Comments
Jan
on 14 Jul 2017
With some guessing:
% 0.0 => 100%, 0.5 => 0%
x = [0, 0.38, 0.5];
p = (0.5 - x) * 2 * 100; % or 100 - 200 * x
set(handles.percentbox,'string', num2str(percent));
If this helps, I cannot avoid to mention that this is very elementary maths and the formulation of the question was misleading.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!