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개)
Fangjun Jiang
2012년 1월 10일
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
Andrew Newell
2012년 1월 10일
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
2012년 1월 10일
Thank you Andrew!
Dmitry
2012년 1월 11일
Fangjun Jiang
2012년 1월 11일
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
2012년 1월 10일
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!