How to find the total quantity of less than or equal to one element in an array?

조회 수: 2 (최근 30일)
I have an array a=[1 2 3 4 1 1 9 3 6]. I want to count the numbers which is less than or equal the element 4 in the array. And then, add those quantity and divide by 10.
Example: The 4th element is 4. There are some other elements in this array which is equal or less than of 4. How can I find the total quantity of it?
How can i do that?
  댓글 수: 2
Image Analyst
Image Analyst 2022년 4월 19일
OK, surely this must be your homework, so we can't just do it for you. First read this:
then invest two hours here so you can do what you asked (which is a very, very basic thing):
After that you'll be able to do it yourself and won't get into trouble for turning in our solution as your own.
Next the count of numbers less than 4 is a scalar (6), so what do you mean by "add those quantity"? What quantity? It's a single number 6 so there is nothing to add to it. Do you want to sum all the values less than 4?
MEHNAZ SHARNA
MEHNAZ SHARNA 2022년 4월 19일
It is not a homework problem. It is just an query for my research. It is a summary of large quantity so that I can get an idea how to solve this.

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

답변 (1개)

Image Analyst
Image Analyst 2022년 4월 19일
Well you didn't answer my questions so I'll just guess at some things
a=[1 2 3 4 1 1 9 3 6]
a = 1×9
1 2 3 4 1 1 9 3 6
indexesLessThan4 = a < 4 % A logical vector of 0 or 1
indexesLessThan4 = 1×9 logical array
1 1 1 0 1 1 0 1 0
% Count the number
count = sum(indexesLessThan4)
count = 6
% Get the actual values
valuesLessThan4 = a(indexesLessThan4)
valuesLessThan4 = 1×6
1 2 3 1 1 3
% Sum those up and divide by 10.
result = sum(valuesLessThan4) / 10
result = 1.1000

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by