how to find value in a matrix from column and row name

조회 수: 2 (최근 30일)
Huda Mohammed
Huda Mohammed 2022년 2월 21일
편집: Cris LaPierre 2022년 2월 21일
Age Height Weight
___ ______ ______
Smith 38 71 176
Johnson 43 69 163
Williams 38 64 131
Jones 40 67 133
Brown 49 64 119
Q: I want the user to input the row name and then column name to get the value intersection between them
for example if the user enter T(smith,age)
the answer will be 38
  댓글 수: 1
Jan
Jan 2022년 2월 21일
What is the shown table? A text file? Or a table? In the latter case, please post the code to create the table instead of this displayed text.

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 2월 21일
편집: Cris LaPierre 2022년 2월 21일
If you are using a table, see this page. You could do something like this
% create a sample table
load patients
T = table(Age,Gender,Height,Weight,Smoker,...
'RowNames',LastName)
T = 100×5 table
Age Gender Height Weight Smoker ___ __________ ______ ______ ______ Smith 38 {'Male' } 71 176 true Johnson 43 {'Male' } 69 163 false Williams 38 {'Female'} 64 131 false Jones 40 {'Female'} 67 133 false Brown 49 {'Female'} 64 119 false Davis 46 {'Female'} 68 142 false Miller 33 {'Female'} 64 142 true Wilson 40 {'Male' } 68 180 false Moore 28 {'Male' } 68 183 false Taylor 31 {'Female'} 66 132 false Anderson 45 {'Female'} 68 128 false Thomas 42 {'Female'} 66 137 false Jackson 25 {'Male' } 71 174 false White 39 {'Male' } 72 202 true Harris 36 {'Female'} 65 129 false Martin 48 {'Male' } 71 181 true
% A couple ways to index using rownames
T{'Smith','Height'}
ans = 71
T.Height({'Smith','Harris'})
ans = 2×1
71 65

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by