필터 지우기
필터 지우기

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

조회 수: 34 (최근 30일)
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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by