Check if a number in a matrix's first column exist. If yes then keep that entire row in a separate matrix

10 views (last 30 days)
I have a 5x3 matrix. I want to scan through the first column of the matrix for a particular number. If that particular number exsit then I want that entire row of the matrix in a sepreate matrix called results.
example: give the matrix
example = [9,8.16,8.17;16,12.36,12.36;23,40.69,27;30,5.66,103.41;9,25.21,122.96]
% 9 8.1600 8.1700
% 16 12.3600 12.3600
% 23 40.6900 27.0000
% 30 5.6600 103.4100
% 9 25.2100 122.9600
if we search for number 9 in the first column (only the first column) then the resultunt new matrix should be
result =
9 8.1600 8.1700
9 25.2100 122.960
Anyhelp would be greatly appreciated. Thank you
best regards

Accepted Answer

KSSV
KSSV on 27 Mar 2019
Edited: KSSV on 27 Mar 2019
A = [ 9 8.1600 8.1700
16 12.3600 12.3600
23 40.6900 27.0000
30 5.6600 103.4100
9 25.2100 122.9600] ;
iwant = A(A(:,1)==9,:)
If you have multiple number to pick use ismember
B = [9 ; 16] ;
[idx,ia] = ismember(A(:,1),B)
iwant = A(idx,:)

More Answers (0)

Categories

Find more on Matrices and 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!