Count number of elements in a matrix more or less than given numbers

168 ビュー (過去 30 日間)
Meg Cullen
Meg Cullen 2019 年 5 月 16 日
回答済み: Keshav Anirudh 2023 年 6 月 22 日
I want to count number of elements in the 2nd column of a matrix more or less than given numbers x and y.
For example, In the matrix A, x=20 and y=10.Thus the count of numbers >20 and <10 (i.e not between 10 to 20) is 5
A =
1 23
2 9
3 55
4 78
5 16
6 11
7 1
How to construct this code?

採用された回答

Kevin Phung
Kevin Phung 2019 年 5 月 16 日
編集済み: Kevin Phung 2019 年 5 月 16 日
x = 10;
y = 20;
sum(A(:,2)<x | A(:,2)>y)

その他の回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 16 日
Hi Meg,
Here are two simple solutions to your problem:
% Given matrix:
A =[ 1 23;
2 9;
3 55;
4 78;
5 16;
6 11;
7 1];
%% 1- way
Anew = A(:,2);
Index1 = find(Anew<10); Index2 = find(Anew>20);
Number1 = numel(Index1)+numel(Index2);
%% 2- way
Number2 = numel(find(A(:,2)<10))+numel(find(A(:,2)>20));
Good luck.

Keshav Anirudh
Keshav Anirudh 2023 年 6 月 22 日
How to do that for the entire matrix, if you wanna find the for a matrix

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by