Table referencing using cell
4 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have two tables:
Tickers
Tickers nbr
________ ___
{'A2M' } 1
{'AAN' } 2
{'AAP' } 3
{'ABB' } 4
{'ABC' } 5
{'ABS' } 6
{'ADP' } 7
{'AEO' } 8
{'AFG1'} 9
{'AGL' } 10
Data
Date Code Open High Low Close Volume
___________ _______ _____ _____ _____ _____ ________
31-Mar-2015 {'A2M'} 0.555 0.595 0.53 0.565 4.816294
01-Apr-2015 {'A2M'} 0.575 0.58 0.555 0.565 4.37666
02-Apr-2015 {'A2M'} 0.56 0.565 0.535 0.555 2.77964
07-Apr-2015 {'A2M'} 0.545 0.55 0.54 0.545 0.392179
08-Apr-2015 {'A2M'} 0.545 0.545 0.53 0.54 0.668446
09-Apr-2015 {'A2M'} 0.54 0.54 0.532 0.535 0.706846
10-Apr-2015 {'A2M'} 0.53 0.535 0.53 0.535 0.175104
13-Apr-2015 {'A2M'} 0.535 0.54 0.53 0.54 0.565808
14-Apr-2015 {'A2M'} 0.54 0.54 0.535 0.54 0.371393
15-Apr-2015 {'A2M'} 0.54 0.55 0.54 0.54 0.925749
I need to create a new column in data called 'CodeRef' that brings the nbr column from the Tickers table into the corresponding code in the data table.
I've tried find, ismember - but for some reason I am missing something.
0 个评论
采纳的回答
Voss
2023-1-6
编辑:Voss
2023-1-6
% some (simplified) tables like yours:
data = table({'a';'b';'c';'c';'a';'b';'b'}, ...
[3;4;6;8;3;5;4],'VariableNames',{'Code','Open'})
Tickers = table({'a';'b';'c'},[1;2;3],'VariableNames',{'Tickers','nbr'})
[ism,idx] = ismember(data.Code,Tickers.Tickers);
data.CodeRef = NaN(size(data,1),1); % in case any data.Code is missing from Tickers.Tickers, its CodeRef will be NaN
data.CodeRef(ism) = Tickers.nbr(idx(ism))
0 个评论
更多回答(1 个)
Stephen23
2023-1-6
The simple MATLAB approach is to use the JOIN() family:
data = table({'a';'b';'c';'c';'a';'b';'b'}, [3;4;6;8;3;5;4],'VariableNames',{'Code','Open'})
Tickers = table({'a';'b';'c'},[1;2;3],'VariableNames',{'Tickers','nbr'})
out = join(data,renamevars(Tickers,'Tickers','Code'))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!