Matlab matrix

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 commentaires

Andrew Newell
Andrew Newell le 10 Jan 2012
Is each ID on a separate line?
Dmitry
Dmitry le 11 Jan 2012
yep , each

Connectez-vous pour commenter.

Réponses (2)

Fangjun Jiang
Fangjun Jiang le 10 Jan 2012

2 votes

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 commentaires

Andrew Newell
Andrew Newell le 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
Fangjun Jiang le 10 Jan 2012
Thank you Andrew!
Dmitry
Dmitry le 11 Jan 2012
Thank you, but i have one more question.
I have 1000 files (studentIDs) every file has two parameters ( subject1 , subject2 )
every parameter look like
...... day | mark
31.07.2011 | 5
31.07.2011 | 3
31.07.2011 | 5
31.07.2011 | 2
31.07.2011 | 5
I need to write a script which can find "5" in subject1, and show me day and StudentID )
Fangjun Jiang
Fangjun Jiang le 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.

Connectez-vous pour commenter.

Andrei Bobrov
Andrei Bobrov le 10 Jan 2012

1 vote

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'))

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Question posée :

le 10 Jan 2012

Community Treasure Hunt

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

Start Hunting!

Translated by