Attached is column vector ht . I would want to find out below what number in ht would constitute 95% of ht ? For example, numbers in ht that are less than 25 constitute 43.78% of ht.

 채택된 답변

Star Strider
Star Strider 2015년 7월 7일
It took me a bit to understand what you wanted, but the result is actually straightforward:
D = load('ht.mat');
ht = D.ans;
ht95idx = fix(0.95*length(ht));
ht95 = ht(ht95idx)
figure(1)
plot(ht, '-r')
hold on
plot(ht95idx, ht95, 'bp')
hold off
grid
xlabel('index')
ylabel('ht')
text(ht95idx-150, ht95, sprintf('95%%‘ht’ at %5.1f \\rightarrow', ht95), 'HorizontalAlignment','right')
The plot of ‘ht’ as a function of its index:

댓글 수: 10

Also, what will be the percentage if I need <=10 of elements of ht
If I understand correctly what you want, the decimal fraction would be:
DecFrc = 10/length(ht);
for the first 10 elements of ‘ht’. The percentage would be 100 times that:
Pct = 100*DecFrc;
AbelM Kusemererwa
AbelM Kusemererwa 2015년 7월 7일
편집: AbelM Kusemererwa 2015년 7월 7일
What I meant was 95% correspond to 33.7. what percentage will 10 give?
I’m still not certain that I understand what you want, but see if this works:
ht10idx = fix(0.10*length(ht));
ht10 = ht(ht10idx)
That’s the same as my previous code, except for the change of constant.
What I meant was 95% correspond to 33.7. what percentage of ht will correspond to 10?
When I run the code I posted in my last Comment, it evaluates ht10=16.7.
This code will create a plot that may help, since instead of plotting ‘ht’ as a function of the index, it plots it as a function of the percentage:
D = load('ht.mat');
ht = D.ans;
idx = 1:length(ht); % Create Index Vector
pct = 100*idx/idx(end); % Calculate Index As Percent
ht95idx = fix(0.95*length(ht)); % Index at 95%
ht95 = ht(ht95idx); % Value at 95%
ht10idx = fix(0.10*length(ht)); % Index at 10%
ht10 = ht(ht10idx); % Value at 10%
figure(1)
plot(pct, ht, '-r')
hold on
plot(pct(ht95idx), ht95, 'bp')
plot(pct(ht10idx), ht10, 'bp')
hold off
grid
xlabel('Percentage')
ylabel('ht')
text(pct(ht95idx-150), ht95, sprintf('95%%‘ht’ at %5.1f \\rightarrow', ht95), 'HorizontalAlignment','right')
text(pct(ht10idx+150), ht10, sprintf('\\leftarrow 10%%‘ht’ at %5.1f', ht10), 'HorizontalAlignment','left')
The plot:
__________________________________
This version of the code will return the value of ‘ht’ for any percent you specify (without the plot):
D = load('ht.mat');
ht = D.ans;
idx = 1:length(ht); % Create Index Vector
pct = 100*idx/idx(end); % Calculate Index As Percent
PrctInp = inputdlg('Enter percent of ‘ht’ desired:', 'Calculate ‘ht’ Percent');
Prct_ht = str2num(PrctInp{:});
ht_Idx = find(pct <= Prct_ht, 1, 'last');
ht_Val = ht(ht_Idx);
msgbox(sprintf('The value of ‘ht’ at %4.1f%%= %4.1f', Prct_ht, ht_Val), 'Answer');
I intend to enter ht desire to obtain the percentage
This should work:
D = load('ht.mat');
ht = D.ans;
idx = 1:length(ht); % Create Index Vector
pct = 100*idx/idx(end); % Calculate Index As Percent
htInp = inputdlg('Enter the ‘ht’ desired:', 'Calculate ‘ht’ Percent');
Des_ht = str2num(htInp{:});
ht_Idx = find(ht <= Des_ht, 1, 'last');
Prct_ht = ~isempty(ht_Idx)*pct(ht_Idx);
if isempty(ht_Idx)
Prct_ht = 0;
end
msgbox(sprintf('The value of ‘ht’ at %4.1f%%= %4.1f', Prct_ht, Des_ht), 'Answer');
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

bio lim
bio lim 2015년 7월 7일
편집: bio lim 2015년 7월 7일
First of all, I think it is bad idea to save your variable as ' ans ', so you should define a unique name. (I have renamed ' ans ' to ' data ' in your ht.mat file.)
This might be a clumsy code but should do the trick.
load('ht.mat')
x = zeros(size(data)); % Create vector of zeros
for i = 1 : length(data)
x(i) = sum(data==data(i)); % Counting the number of occurance
end
z = [data x]; % Matrix with first column as your initial data, and second as the number of occurence.
h = unique(z, 'rows'); % Only unique elements are selected.
sum = cumsum(h(:,2)); % 95% of 23880 is 22686
g = [h sum];
Thus less than 34.7 could give you 95% (More precisely 95.017%) . Hope it helps.
Sean de Wolski
Sean de Wolski 2015년 7월 7일
편집: Sean de Wolski 2015년 7월 7일
You should be able to use prctile for this.
x = rand(100,1);
xp = prctile(x,[0,95]); % 2nd element
plot(sort(x))
line([1 100],xp([2 2]))
title(['95% is ' num2str(xp(2))])

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by