How can I use one row having full data whose elements repeat [like A(:,1) below] to query another data of the same type but having some elements missing?
Show older comments
I have this array say
A =
[0 10
0 20
1 30
1 40
2 50
2 60
3 70
3 80
4 90
4 100
5 10
5 20]
-----------------------------------
B =
[0 110
0 210
1 300
1 405
2 606
3 707
3 801
4 100
5 204]
I want A and B to have the same length by comparing and inserting 'NAN' to rows in B that are missing.
Thus to produce an output like this where the NAN is inserted;
B =
[0 110
0 210
1 300
1 405
NAN NAN
2 606
3 707
3 801
NAN NAN
4 100
NAN NAN
5 204]
Thank you in advance :)
Accepted Answer
More Answers (1)
Dyuman Joshi
on 6 Feb 2023
Edited: Dyuman Joshi
on 6 Feb 2023
A = [0 10; 0 20; 0 25; 1 30; 1 40; 2 50; 2 60; 3 70; 3 80; 4 90; 4 100; 5 10; 5 20; 5 35];
B = [0 110; 0 210; 1 300; 1 405; 2 606; 3 707; 3 801; 4 100; 5 204];
sA=size(A);
sB=size(B);
B = [B; NaN(sA(1)-sB(1),sB(2))];
for idx=1:sA(1)
if A(idx,1)~=B(idx,1)
ct=find(B(:,1)==A(idx,1),1);
B=B([1:ct-1 end ct:end-1],:); %shifting NaN row
end
end
B
Note - You should ideally use tolerance to compare two values, abs(val1-val2)<tol
2 Comments
Stephen Tete
on 6 Feb 2023
Dyuman Joshi
on 6 Feb 2023
Edited: Dyuman Joshi
on 6 Feb 2023
@Stephen Tete, I have modified my answer for a more general case (and a working example as well)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!