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 个评论

Is each ID on a separate line?
Dmitry
Dmitry 2012-1-11
yep , each

请先登录,再进行评论。

回答(2 个)

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 个评论

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)
Thank you Andrew!
Dmitry
Dmitry 2012-1-11
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 )
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.

请先登录,再进行评论。

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

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

提问:

2012-1-10

Community Treasure Hunt

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

Start Hunting!

Translated by