Count the numbers in an array until a certain number

8 ビュー (過去 30 日間)
cestcandice
cestcandice 2017 年 6 月 7 日
編集済み: Stephen23 2017 年 6 月 7 日
I have an array of numbers, the user can input whatever number they like. Long story short, I determined the most highly composite number in an array of numbers from 1 to whatever number the user chooses.
Once I find the most highly composite number, in the case of 12-- which is in the code below-- it's 6, I need to find what number 6 corresponds to (which in this case is 12).
CONCLUSION: I need to count the amount of numbers in the original array that is less than the number I output (in this case 6).
It seems like a lot of people are able to count every number in an array less than a certain number, * but I need to figure out how I can count every number in array BEFORE a certain number.*
prompt={'Enter any value'};
name = 'Any Value';
defaultans = {'12'};
options.Interpreter = 'tex';
B = inputdlg(prompt,name,[1 30],defaultans,options);
% Get the value out of a cell
a = str2num(cell2mat(B));
% Make an numeric array from 1 to a
K = 1:a
for t=1:length(K);
K=rem(t,1:t);
b(t)=sum(K==0);
end
y1=reshape(b,[],length(K))
m=max(y1)
%here I find the the max number in y1 is 6, now I need to be able to count
%every number only up until 6.

採用された回答

cestcandice
cestcandice 2017 年 6 月 7 日
Never mind! I found the solution to my own problem... For anyone else curious, here's what you can do!
%find the location of the maximum number
n=find(y1==m);
%display the first time that number appears
n(1)
  1 件のコメント
Stephen23
Stephen23 2017 年 6 月 7 日
編集済み: Stephen23 2017 年 6 月 7 日
Or:
find(y1==m,1,'first')

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by