mHelpMe
mHelpMe

Reputation: 6668

resizing matrix and matching data

I have a matrix called universe with n rows and 2 columns. This matrix is the universe of books. I then have another matrix this time with m rows (where m is less than or equal to n) and 2 columns. This matrix can be thought of as a book store.

Both matrices contain a column Book ISIN all the entries are unique.

Edit

Please note the book ISIN contains letters and numbers.

End of edit

What I would like to do is resize my Book Store matrix so the Book ISIN is exactly the same as the Book ISIN column in my Universe Matrix.

Lastly I then want any book ISIN that wasn't in the book store to have a zero value in the Total Sold column. The last matrix on the right below is the result I'm looking for.

n >= m

Matrix Universe (n x 2)         Book Store (m x 2)                Result I would like

Book ISIN   Book Name           Book ISIN    Total Sold           Book ISIN    Total Sold
12DE3         ABC               45ES6          3                  12DE3          0
45ES6         DEF               10SX1          9                  45ES6          3
78AQ9         FGH                                                 78AQ9          0 
10SX1         LMN                                                 10SX1          9

Upvotes: 1

Views: 76

Answers (1)

Divakar
Divakar

Reputation: 221514

Code

%%// Given data
universe  = {
    '12DE3' 'ABC';
    '45ES6' 'DEF';
    '78AQ9' 'FGH';
    '10SX1' 'LMN'}
store = {
    '10SX1' '9';
    '45ES6' '3'}

%%// Detect the row numbers of `universe` where data from first column 
%%// of `store` is present in first column of `universe`
[true_pos,pos1] = ismember(universe(:,1),store(:,1));

%%// Final output which is same as universe array 
%%// but second is modified based on store
store_mod = universe;
store_mod(:,2)={'0'};
store_mod(true_pos,2) = store(pos1(pos1~=0),2)

Output

store_mod = 

    '12DE3'    '0'
    '45ES6'    '3'
    '78AQ9'    '0'
    '10SX1'    '9'

Upvotes: 1

Related Questions