How to create a matrix from 2 matrices with specific values

1 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 21 Jan 2019
@Ivas: please show an example of the output matrix, for the data that you have uploaded.
lvas
lvas on 21 Jan 2019
I updated the picture showing a hand written output matrix of the first rows

Sign in to comment.

Answers (1)

Luna
Luna on 21 Jan 2019
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.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!