How to get rows with all similar columns and adjust matrix with shorter length to that of longer length
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I have two example arrays here (Time vectors in format [Y M D H M S]) as follows (my original data has 73200 sample points)
A = [2023 6 29 7 8 9; 2023 6 29 7 8 10; 2023 6 29 7 8 11; 2023 6 29 7 8 12; 2023 6 29 7 8 18; 2023 6 29 7 8 19; 2023 6 29 7 8 20; 2023 6 29 7 8 21; 2023 6 29 7 8 22; 2023 6 29 7 8 23; 2023 6 29 7 8 24]
B = [2023 6 29 7 8 22.5; 2023 6 29 7 8 23; 2023 6 29 7 8 24]
And a data vector for matrix B:
B_data = [12 21 21] (To note, A matrix also has a data vector but it is not the part of the problem)
What i am planning to do is to is to allign both the vectors to get same length i.e. to fill in the missing time data in B and to make it same length as A.
Here was my effort: I tried to find the rows with common columns and for these rows i kept the origianl data of B, and for the missing rows i added those from A.
I used (ismember(B,A,rows)) to get the index.
What is confusing to me is that when i use (ismember(B,A,rows)) and (ismember(A,B,rows)), i am not getting the same number of elements.
Because of this the end result i want is not correct. Can someon help me out here. I thank you in advance.
2 个评论
Dyuman Joshi
2023-10-12
编辑:Dyuman Joshi
2023-10-12
"Here was my effort: I tried to find the rows with common columns and for these rows i kept the origianl data of B, and for the missing rows i added those from A."
"Because of this the end result i want is not correct."
You are assuming that A and B will exactly have size(A,1)-size(B,1) different rows.
What if A and B have less than size(A,1)-size(B,1) different rows? or more than that? What should be the output then?
Given the data, what is the expected result? And what is the logic/criteria behind achieveing that result?
Edit - Changed the incorrect numel(x) to size(x,1) for number of rows
回答(1 个)
Sulaymon Eshkabilov
2023-10-12
If understood correctly or presuming it :), is this what you are try to get:
A = [2023 6 29 7 8 9;
2023 6 29 7 8 10;
2023 6 29 7 8 11;
2023 6 29 7 8 12;
2023 6 29 7 8 18;
2023 6 29 7 8 19;
2023 6 29 7 8 20;
2023 6 29 7 8 21;
2023 6 29 7 8 22;
2023 6 29 7 8 23;
2023 6 29 7 8 24]
B = [2023 6 29 7 8 22.5;
2023 6 29 7 8 23;
2023 6 29 7 8 24]
ID_miss=ismember(A,B,'rows') % This is what finds out what rows are missing in B
IDX = find(ID_miss==0);
B(IDX, :)=A(IDX,:) % Fills up with the data in A like equating B to A
1 个评论
Dyuman Joshi
2023-10-12
The size of the final output should be the same as the size of A.
"What i am planning to do is to is to allign both the vectors to get same length i.e. to fill in the missing time data in B and to make it same length as A."
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!