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!