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
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?
回答 (1 件)
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]
indexesLessThan4 = a < 4 % A logical vector of 0 or 1
% Count the number
count = sum(indexesLessThan4)
% Get the actual values
valuesLessThan4 = a(indexesLessThan4)
% Sum those up and divide by 10.
result = sum(valuesLessThan4) / 10
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!