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
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
Sachin Hegde
Sachin Hegde 2023-10-12
Hi,
Let me add some more information, There are no repeated rows in A , and the same goes for B. This eliminates the different rows being more than numel(A) - numel (B).
Also i am not assuming different rows = numel(A) - numel (B). This can be seen in the example as well. In th elast column, A has a value of 22 and B 22.5, and because of this the number of different rows = 9 ~= numel(A) - numel(B).
In this case i would still keep with A. For any row of A which is not in B shall be replaced with that of A, and any extra row of B which is not in A shall be deleted in order to make it same length as A.
i hope this clears up the problem even more.

请先登录,再进行评论。

回答(1 个)

Sulaymon Eshkabilov
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]
A = 11×6
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
B = [2023 6 29 7 8 22.5;
2023 6 29 7 8 23;
2023 6 29 7 8 24]
B = 3×6
1.0e+03 * 2.0230 0.0060 0.0290 0.0070 0.0080 0.0225 2.0230 0.0060 0.0290 0.0070 0.0080 0.0230 2.0230 0.0060 0.0290 0.0070 0.0080 0.0240
ID_miss=ismember(A,B,'rows') % This is what finds out what rows are missing in B
ID_miss = 11×1 logical array
0 0 0 0 0 0 0 0 0 1 1
IDX = find(ID_miss==0);
B(IDX, :)=A(IDX,:) % Fills up with the data in A like equating B to A
B = 9×6
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
  1 个评论
Dyuman Joshi
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 CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by