How can I find indices of elements bigger or smaller than a value in different columns?
34 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Image Analyst
on 8 Dec 2019
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 Comments
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!