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