Matlab matrix
Show older comments
I have a question I have the matrix (example)
ID1 54 65
ID2 45 48
ID3 23 43
ID4 32 24
ID5 75 23
ID6 87 45
(real size is about 2000 strings) I need to find a string where 23 43 and after that show ID (there maybe several IDs)
thank you Dmitry
Answers (2)
Fangjun Jiang
on 10 Jan 2012
Along the line of this code, depending on your data format.
ID={'ID1','ID2','ID3','ID5','ID6'}.';
Col2=[54 45 23 32 75 87].';
Col3=[65 48 43 24 23 45].';
Index=and(Col2==23,Col3==43);
SelectedID=ID(Index);
4 Comments
Andrew Newell
on 10 Jan 2012
But first you need to read it from a file, I presume. You could use something like this:
fid = fopen('testfile.m');
C = textscan(fid,'%s %d %d');
fclose(fid);
ID = C{1}; Col2 = C{2}; Col3 = C{3};
Index=and(Col2==23,Col3==43);
SelectedID=ID(Index);
(Note there is a typo in Fangjun's second last line)
Fangjun Jiang
on 10 Jan 2012
Thank you Andrew!
Dmitry
on 11 Jan 2012
Fangjun Jiang
on 11 Jan 2012
It is similar. Once you read in the data, use logical index, index=subject1==5, or linear index, index=find(subject1==5), and then FoundStudent=StudentID(index). find() also have options to find first, or last, or any number of instance.
Andrei Bobrov
on 10 Jan 2012
ID = {'ID1'
'ID2'
'ID3'
'ID4'
'ID5'
'ID6'}
data = [54 65
45 48
23 43
32 24
75 23
87 45]
out = ID(ismember(data,[23 43],'rows'))
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!