필터 지우기
필터 지우기

How to align multiple matrices based on the first column values?

조회 수: 1 (최근 30일)
Shirley
Shirley 2019년 3월 18일
편집: Shirley 2019년 3월 18일
Here is an example
A = [1,4;2,6;2,7;3,5;3,6;5,2];
B = [2,5;4,6;4,7;5,3];
I want the output to be like (3 columns): (combining the 2nd column of A and B based on their 1st column values)
1 4 Na
2 6 5
2 7 5
3 5 Na
3 6 Na
4 Na 6
4 Na 7
5 2 3
But in reality I have more than 2 datasets that I want to reorganize and combine like the example showing before. Thanks

채택된 답변

Guillaume
Guillaume 2019년 3월 18일
The simplest way is convert your matrices to a table, which may make sense anyway considering that it looks like the columns of your matrices have different meaning. Then simply outerjoin the two tables. e.g:
A = [1,4;2,6;2,7;3,5;3,6;5,2];
B = [2,5;4,6;4,7;5,3];
tA = array2table(A, 'VariableNames', {'Key', 'ValA'});
tB = array2table(B, 'VariableNames', {'Key', 'ValB'});
tC = outerjoin(tA, tB, 'MergeKeys', true)
For multiple tables, repeat the join in a loop.
  댓글 수: 2
Shirley
Shirley 2019년 3월 18일
That is a very clever way!!! It works very well. Thank you so much.
Shirley
Shirley 2019년 3월 18일
편집: Shirley 2019년 3월 18일
I am sorry that I made a mistake in the question before. Actually, when the Key(first column)=2, I want the row to be:
2 6 5
2 6 NaN
How should I do that then?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by