How to create a matrix from 2 matrices with specific values
1 次查看(过去 30 天)
显示 更早的评论
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.
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
2019-1-21
@Ivas: please show an example of the output matrix, for the data that you have uploaded.
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!