how to filter data based on one value in a column?

83 views (last 30 days)
how to filter data based on one value in a column. i have 40000 rows and 4 columns. in 4th column i have values 1, 2, 3, 4. i want to filter all records in which the values of the 4th column should only be 3. the data is 40000 X 4 double type.

Answers (1)

Cris LaPierre
Cris LaPierre on 1 Nov 2021
use a logical array (Ch 12 in MATLAB Onramp).
A = rand(10,3);
A(:,4) = randi(4,10,1)
A = 10×4
0.7596 0.0937 0.1802 3.0000 0.1846 0.3415 0.3006 4.0000 0.2324 0.8903 0.4734 4.0000 0.8114 0.1004 0.7153 3.0000 0.5624 0.9496 0.5770 1.0000 0.3444 0.0529 0.0754 2.0000 0.1635 0.0971 0.6925 1.0000 0.6909 0.6383 0.7955 3.0000 0.5133 0.3658 0.8523 2.0000 0.1696 0.2123 0.9565 2.0000
idx = A(:,4)==3;
B = A(idx,:)
B = 3×4
0.7596 0.0937 0.1802 3.0000 0.8114 0.1004 0.7153 3.0000 0.6909 0.6383 0.7955 3.0000

Community Treasure Hunt

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

Start Hunting!