Display value from one table next to a certain name in another table

1 ビュー (過去 30 日間)
Emiliya Taskova
Emiliya Taskova 2021 年 1 月 13 日
コメント済み: Emiliya Taskova 2021 年 1 月 13 日
I have a very long data set with two tables that I'm going to give shortened examples of to explain what I'm trying to do. In Table 1 I have the following names:
and the following values corresponding to each of these members:
In another, longer table I have more names, some of which are found in table 1 but not all. For example:
I want to display the value of each name in table 1 next to the matching name in table 2 but I'm having trouble doing that because of the different number of rows. I'd really appreciate some help!
  2 件のコメント
Adam Danz
Adam Danz 2021 年 1 月 13 日
Sounds like you're looking for a vlookup which is just a matter of indexing and strcmp or ismember in Matlab.



Iuliu Ardelean
Iuliu Ardelean 2021 年 1 月 13 日
編集済み: Iuliu Ardelean 2021 年 1 月 13 日
I'm assuming your variables will look like this:
names_1 = ['AAA'; 'BBB'; 'CCC'];
values_1 = [834240000; 9951600; 2383500];
names_2 = ['GGG'; 'ZZZ'; 'MMM'; 'AAA'; 'QQQ'; 'FFF'; 'BBB'; 'YYY'];
values_2 = zeros(length(names_2), 1); % initialize values_2 as column of zeros for now
% find which members in names_2 also appear in names_1 and where they are in names_1
[Lia, Lib] = ismember(names_2, names_1, 'rows')
values_2(Lia) = values_1(Lib(Lib ~= 0)) % assign values accordingly
Lia will look like [0, 0, 0, 1, 0, 0, 1, 0]
and Lib will look like [0, 0, 0, 1, 0, 0, 2, 0]
and values_2 will look like [0, 0, 0, 834240000, 0, 0, 9951600, 0]
Check out ismember if you wish.
Hope this helps.
NOTE: if you run into trouble you could try to substite zeros with
values_2 = nan(length(names_2), 1)
  1 件のコメント
Emiliya Taskova
Emiliya Taskova 2021 年 1 月 13 日
Thank you so much! Everything worked now and I managed to get the output that I wanted!


その他の回答 (0 件)


Community Treasure Hunt

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

Start Hunting!

Translated by