How can I find indices of elements bigger or smaller than a value in different columns?
48 次查看(过去 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?
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!