# how to find index of the point in matrix with condition?

100 views (last 30 days)
ha ha on 28 Aug 2017
Edited: Jan on 28 Aug 2017
Let's say, i have matrix 6-by-1 matrix:
A=[ 1 --->labelling "1"
6 --->labelling "2"
3 --->labelling "3"
5 --->labelling "4"
-3 --->labelling "5"
10 ] --->labelling "6"
and
B=
I want to write the code to compare each value in matrix A with B with condition ( If {A}<B---> I will pick up the index and the value from matrix A to make a new matrix)
Particurly: I have the number 1,3, -3 (in matrix A) is smaller than 5 (in matrix B) so I will have the result after compare condition as follows:
C=[ 1 --->labelling "1"
3 --->labelling "3"
-3 ] --->labelling "5"
and the labelling matrix D will be :
D=[ 1
3
5 ]
How to write a relationship between matrix A,B,C,D ?
KL on 28 Aug 2017

Benjamin Imbach on 28 Aug 2017
Edited: Benjamin Imbach on 28 Aug 2017
check out the find function.
A = [1 6 3 5 -3 10]';
B = 5;
D = find(A<B); %(linear index)
C = A(D);
Jan on 28 Aug 2017
This is even more efficient with "logical indexing": omit the "find".
C = A(A < B)