Create a matrix table from two matrices

Hi;
I have to matrices (5x2) of the same dimensions.
A= [10 0;
8 2;
6 2;
4 2;
1 1];
B= [10 0;
9 1;
8 1;
7 1;
6 1];
I need to create a new matrix C where the first columns of matrices A and B are merged in a single column in C with decresing order while eliminating repeated values. The new matrix C will include aditional columns to save the corresponding values of A and B. Set the element to 0 If there is not a corresponding value. After merging A and B, the new matrix C is a (7x3) and looks in the following way:
A(:,1)/B(:,1) A(:,2) B(:,2)
C= [ 10 0 0
9 0 1
8 2 1
7 0 1
6 2 1
4 2 0
1 1 0]
Many thanks
Eliot

 採用された回答

madhan ravi
madhan ravi 2019 年 4 月 6 日
編集済み: madhan ravi 2019 年 4 月 6 日

0 投票

% Assuming first column of A & B is already arranged in descending order
U = sort(unique([A(:,1);B(:,1)]),'descend');
[AA,BB]=deal(zeros(size(U)));
AA(ismember(U,A(:,1)))=A(:,2);
BB(ismember(U,B(:,1)))=B(:,2);
C = [U,AA,BB];

その他の回答 (1 件)

Eliot Motato
Eliot Motato 2019 年 4 月 6 日

0 投票

Many, thanks Madhan
It works perfectly!
Regards
Eliot

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by