How to create a matrix from 2 matrices with specific values

조회 수: 5 (최근 30일)
lvas
lvas 2019년 1월 21일
댓글: lvas 2019년 1월 22일
first of all I am sorry if the title is vague but I was not sure on how to describe what I am trying to do.
As you can see in this picture I have 2 seperate Matrices which I also have attached to this post, after the black line is how i would like the result to be.
result.jpg
The first column of each matrix shows the ID while the second the time (disregard the third).
EXAMPLE
ID = 3 , starts at 161 and finishes at 250.
My goal is to create one matrix where all values are there.
My problem is that there are cases that in some IDs there is a starting time but not an ending time and in some other IDs there are no starting time but only ending time.
EXAMPLE
ID = 7, starts at 182 and finishes at 250. AND starts at 298 and never finishes.
ID = 11, NEVER starts but finishes at 51. AND then starts at 187 and finishes at 261. AND starts at 305 and finishes at 361.
So i would like this cases where it never ends or it never starts to be in the new matrix with zeros when it does not exist.
I have made several attempts to make this but I am not approaching this correctly.
Any help is appreciated.
  댓글 수: 2
Stephen23
Stephen23 2019년 1월 21일
@Ivas: please show an example of the output matrix, for the data that you have uploaded.
lvas
lvas 2019년 1월 21일
I updated the picture showing a hand written output matrix of the first rows

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

답변 (1개)

Luna
Luna 2019년 1월 21일
Hi Ivas,
Outer join of tables would help but you should define a rule or a condition that how to choose when we have multiple IDs.
This approach only works very well when your IDs are unique. You can get it wih using unique function ofc, but still you need choose which row you are going to select and which one you will delete.
Maybe you would like to post process C1 or C2 tables below like that: if start a point is smaller than corresponding finish point you can delete that row.
Here is my piece of code for you:
A = load('1.mat');
B = load('2.mat');
tableStart = array2table(A.overall,'VariableNames',{'ID' 'var1' 'var2'});
tableFinish = array2table(B.overall2,'VariableNames',{'ID' 'var3' 'var4'});
C1 = outerjoin(tableStart,tableFinish,'MergeKeys',false); % merged with ID column.
C2 = outerjoin(tableStart,tableFinish,'MergeKeys',true); % not merged with ID column.
  댓글 수: 1
lvas
lvas 2019년 1월 22일
thank you for this answer, I will try it in the next hours

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

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by