Replace elements in array

3 views (last 30 days)
Oscar
Oscar on 24 Oct 2014
Commented: Oscar on 24 Oct 2014
I have a 1 048 000 by 7 cell array, A, with stock data (one row for every stock and time), all values are numbers apart from the identifying ISIN-numbers which are strings (looking like 'AB00063033' etc.). I have created another 700 by 2 cell array, B, with two columns, one with ISIN-numbers and one with a corresponding identifying number. I want to replace the ISIN-numbers in the larger array with the corresponding identifying numbers from the smaller array.
Essentially doing changem(A,B(:,2),B(:,1)), but for a cell array instead of a matrix.
I have done a small for-loop that solves this, but it takes an eternity with over a million elements to check. Are there any easy commands that solves my problem?
Thanks!
  1 Comment
Azzi Abdelmalek
Azzi Abdelmalek on 24 Oct 2014
This is not clear, illustrate with a small example, show the expected result

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 24 Oct 2014
Use the second output of ismember for that:
a = {1 2 3 4 5 6 'ABxxxx';
8 9 10 11 12 13 'CDxxxx';
15 16 17 18 19 20 'ABxxxx';
22 23 24 25 26 27 'EFxxxx'};
b = {'ABxxxx' 12345;
'CDxxxx' 78910;
'EFxxxx' 98765}
[found, idx] = ismember(a(:, 7), b(:, 1)); %match column 7 of a with column 1 of b
if ~all(found)
error('b is missing some ISIN')
end
a(:, 7) = b(idx, 2); %replace column 7 of a with column 2 of b

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!