Select rows in a table given two conditions

77 ビュー (過去 30 日間)
Max Bornemann
Max Bornemann 2019 年 4 月 5 日
コメント済み: Guillaume 2019 年 4 月 5 日
Hello everyone. I have the following table named TAB1:
Description Year Value
'Steam Coal EU' 2045 78.5000000000000
'Steam Coal EU' 2050 80
'Steam Coal EU' 2055 81.5000000000000
'CO2' 2015 7.62553571428572
'CO2' 2020 8.87000000000000
'CO2' 2025 19.2558584219015
I`m searching for a way to get the Value of CO2 and 2020 (8.87000000000000). My original table is way bigger and might be completed later, so hard coding like TAB1{4,3} is not what i`m searching.
I have the following approach:
TAB1{strcmp(TAB1,'CO2'),'Value'})
This gives me all values with CO2 on the left - but i just want the second value. Can someone help? Thanks a lot.

採用された回答

Guillaume
Guillaume 2019 年 4 月 5 日
I'm not conviced that your TAB1 is actually a table.
If it is:
TAB1.Value(strcmp(TAB1.Description, 'CO2') & TAB1.Year == 2020)
Note that if you're using strings instead of char arrays in the Description variable, then you can use == instead of strcmp:
TAB1.Value(TAB1.Description == "CO2" & TAB1.Year == 2020)
  2 件のコメント
Max Bornemann
Max Bornemann 2019 年 4 月 5 日
Thank you Guillaume! Especially the second hint is nice to know. I also found a different way:
TAB1{strcmp(TAB1.Description,'CO2'),'Value'}(2,1)
Guillaume
Guillaume 2019 年 4 月 5 日
TAB1{strcmp(TAB1.Description,'CO2'),'Value'}(2,1)
only works if 2020 is the 2nd row of all the 'CO2' rows. If you want to use that syntax the proper equivalent is:
TAB1{strcmp(TAB1.Description, 'CO2') & TAB1.Year == 2020, 'Value'}
You can indeed access the content of a table with either {} indexing or . indexing. I find . indexing easier to read. If you went {} indexing the whole way, it would read as:
TAB1{strcmp(TAB1{:, 'Description'}, 'CO2') & TAB1{:, 'Year'} == 2020, 'Value'}

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

その他の回答 (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