Mostrar comentarios más antiguos
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
2 comentarios
Andrew Newell
el 10 de En. de 2012
Is each ID on a separate line?
Dmitry
el 11 de En. de 2012
Respuestas (2)
Fangjun Jiang
el 10 de En. de 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 comentarios
Andrew Newell
el 10 de En. de 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
el 10 de En. de 2012
Thank you Andrew!
Dmitry
el 11 de En. de 2012
Fangjun Jiang
el 11 de En. de 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
el 10 de En. de 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'))
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!