Table referencing using cell
2 ビュー (過去 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!