How to write a code that counts how many numbers inside a vector is less than a specific vector?

So i have a randomily distrubited number as follows:
for i=1:1000
k(i)=unifrnd(1,20);
end
i want to write a code that checks how many numbers inside k are larger than 1, then how many larger than 2, then how many larger than 3 and so on until how many are larger than 19.
Thank you in advance.

回答(1 个)

Try this —
k = unifrnd(1,20, 1, 1000);
for k1 = 1:19
Exceeds(k1,:) = nnz(k>k1);
end
Value = 1:19;
DesiredResult = table(Value(:),Exceeds)
DesiredResult = 19×2 table
Var1 Exceeds ____ _______ 1 1000 2 958 3 903 4 842 5 777 6 725 7 679 8 622 9 559 10 504 11 450 12 390 13 335 14 281 15 217 16 163
figure
plot(Value, Exceeds)
grid
.

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by