Find elements in a matrix

1 回表示 (過去 30 日間)
Hanna Sundling
Hanna Sundling 2019 年 11 月 14 日
コメント済み: Guillaume 2019 年 11 月 14 日
The task is to find how many of the elements in A is numbers between 30 and 65, how do I find that? My code looks like this:
Skärmavbild 2019-11-14 kl. 15.23.15.png
  1 件のコメント
Daniel M
Daniel M 2019 年 11 月 14 日
You can read about logical operators here

サインインしてコメントする。

回答 (2 件)

M
M 2019 年 11 月 14 日
編集済み: M 2019 年 11 月 14 日
You can get the indices with:
idx = A >= 30 & A <= 65
To know the number of values corresponding to the condition:
numel(find(idx))
  1 件のコメント
Guillaume
Guillaume 2019 年 11 月 14 日
nnz(idx)
is simpler and faster than numel(find...)

サインインしてコメントする。


Ruger28
Ruger28 2019 年 11 月 14 日
This really isnt code, or even an attempt....but
A = randi([10,100],8,20);
B = A(A>=30 & A <= 65); % logically index A using your limits
using FIND
A = randi([10,100],8,20);
C = find(A>=30 & A<=65); % get index of values in your window
D = A(C); % get values in A

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by