フィルターのクリア

How can I find indices of elements bigger or smaller than a value in different columns?

68 ビュー (過去 30 日間)
Sevil Cansu Yildirim
Sevil Cansu Yildirim 2019 年 12 月 8 日
回答済み: Image Analyst 2019 年 12 月 8 日
I'm trying to find indices of some elements in matrix A that have 6 columns. I'm only interested on first two columns that having the condition 43>A(:,1) >18 and 43>A(:,2)>30. I wrote following code to achieve it but;
for i = 1:length(a)
ind = find((43 > a(i,1) & a(i,1) > 18) & (43 > a(i,2) & a(i,2) > 30));
if (ind>0)
for j = 1:length(ind)
africa(k,:) = [a(ind(j),1) a(ind(j),2) a(ind(j),3) a(ind(j),4) a(ind(j),5) a(ind(j),6)];
k = k + 1;
end
end
end
it finds only the first element having this condition and write same value in africa. I want africa matrix to have every row that held the condition.
How can I resolve this problem?

回答 (1 件)

Image Analyst
Image Analyst 2019 年 12 月 8 日
You can get a logical map of all indexes where this criteria is true this way:
% A = randi(70, 6, 6) % Create sample data.
col1Mask = A(:, 1) > 18 & A(:, 1) < 43;
col2Mask = A(:, 2) > 30 & A(:, 2) < 43;
mask = [col1Mask, col2Mask]
You should be able to do whatever else you need to do with the logical mask.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by