Search for a value in a table and get the corresponding value from the 2nd column

4 ビュー (過去 30 日間)
Hi guys,
I have a table of values and would like to search for a value and then get the corresponding value from the 2nd column. Can anyone help me with this problem?
Thanks a lot!

採用された回答

Star Strider
Star Strider 2023 年 4 月 2 日
If you are searching for an exact match, try something like this —
Col1 = linspace(0, 10, 21).';
Col2 = randn(numel(Col1),1);
T1 = table(Col1,Col2)
T1 = 21×2 table
Col1 Col2 ____ _________ 0 0.38307 0.5 -0.72796 1 1.4325 1.5 -2.2297 2 -0.3249 2.5 -0.065509 3 -1.1099 3.5 1.7075 4 -0.86512 4.5 -0.028325 5 1.4587 5.5 -2.3416 6 0.2667 6.5 0.060289 7 1.6819 7.5 0.077313
Val = 3;
Result = T1{Col1==Val,:}
Result = 1×2
3.0000 -1.1099
Other options, for example using ismember or ismembertol are also useful.
Much depends on the table and what you want to do. This is simply an illustration of the most obvious approach.
.
  2 件のコメント
drummer
drummer 2024 年 1 月 20 日
@Star Strider, how would I do that but rather comparing cells?
Star Strider
Star Strider 2024 年 1 月 20 日
If the data are in a cell array, use cellfun with the appropriate function (for example isequal) or whatever is appropriate for your data (for example strcmp or its friends).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by