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

51 次查看(过去 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 中查找有关 Loops 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