How to get a script to only give me the number of elements less than 2?

조회 수: 1 (최근 30일)
I have a script in which y equals 100 various numbers. I was able to sort the numbers in order from least to greatest, but I need the script to give me the amount of numbers that are less than 2. How would I go about doing this?

채택된 답변

David Hill
David Hill 2020년 1월 9일
nnz(y<2);
  댓글 수: 4
Kyle Donk
Kyle Donk 2020년 1월 9일
But it would make sense for y to change each time I run the script, right? Because if y is a set of 100 random numbers, those numbers would change each time.
David Hill
David Hill 2020년 1월 9일
But each time you run the script,
z=nnz(y<2);
will provide you the number of values of y <2

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

추가 답변 (1개)

Meg Noah
Meg Noah 2020년 1월 9일
Here are random numbers as an example. It plots all the values in blue, finds indices for values less than 2, and plots those values in red.
x = 1:100;
y = 20*rand(100,1)-10;
ind = find(y < 2);
figure()
plot(x,y,'.b');
hold on
plot(x(ind),y(ind),'.r');
  댓글 수: 2
Kyle Donk
Kyle Donk 2020년 1월 9일
This is good, but I need the script to only display how many numbers are less than two. I am not allowed to count the numbers manually.
So if there are five numbers less than two, I need the script to give me "5".
Meg Noah
Meg Noah 2020년 1월 9일
편집: Meg Noah 2020년 1월 9일
to display on the console, just use num2str or fprintf
num2str(numel(ind))
fprintf(1,'There are %d values less than 2\n',numel(ind))

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

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by