Could you please tell me algorithm how to chose numbers in a matrix which is sastified a condition?

for example,i have a matrix a=|1 2 3;3 4 6;8 9 13| i want to chose value numbers between 2 and 5, so i get a12=2;a13=3;a21=3;a22=4;I also know the address of number. But with large matrix, how to get? thanh you so much

 Accepted Answer

Luc, use
a = [1 2 3;3 4 6;8 9 13];
[row col] = find(a>= 2 & a<=5);
which will give you the row and colum indices of the numbers you are looking for. To filter out the individual values and assign them to a new matrix, you could use
aa = a(find(a>= 2 & a<=5));

2 Comments

It is fast and nice!Could you please tell me your algorithm?
This code snippet does the same thing and should give you enough to get started:
a = [1 2 3;3 4 6;8 9 13];
llim = 2;
ulim = 5;
kk = 1;
for ii = 1:length(a(:,1)) % row index
for jj = 1:length(a(1,:)) % col index
if a(ii,jj) >= llim && a(ii,jj) <= ulim
val_ind(kk,:) = [ii jj a(ii,jj)];
kk = kk + 1;
end
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 27 Mar 2014

Commented:

on 27 Mar 2014

Community Treasure Hunt

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

Start Hunting!