How can I change rows that appeared more than once to missing rows

3 views (last 30 days)
Please I need a help!!
I have the set of vectors below. In the second column, 5.0000 and 6.0000 appeared twice, and between 9.0000 and 12.0000, 10.0000 and 11.0000 are missing.
Please how can I change the rows with 5.0000 and 6.0000 to 10.0000 and 11.0000 that are missing and sort the second column in ascending with respect to their corresponding rows.
Please, I need a solution that is applicable to related problems.
2005.00 1.00000 0.000000 0.000000 0.000000
2005.00 2.00000 0.000000 0.000000 0.000000
2005.00 3.00000 0.000000 0.000000 0.000000
2005.00 4.00000 0.000000 0.000000 0.000000
2005.00 5.00000 0.000000 0.000000 0.000000
2005.00 6.00000 0.000000 0.000000 0.000000
2005.00 5.00000 56.0000 4464.00 1.25448
2005.00 6.00000 2937.00 4320.00 67.9861
2005.00 7.00000 2974.00 4464.00 66.6219
2005.00 8.00000 2097.00 4464.00 46.9758
2005.00 9.00000 797.000 4320.00 18.4491
2005.00 12.0000 191.000 4464.00 4.27867
This is my desired output
2005.00 1.00000 0.000000 0.000000 0.000000
2005.00 2.00000 0.000000 0.000000 0.000000
2005.00 3.00000 0.000000 0.000000 0.000000
2005.00 4.00000 0.000000 0.000000 0.000000
2005.00 5.00000 56.0000 4464.00 1.25448
2005.00 6.00000 2937.00 4320.00 67.9861
2005.00 7.00000 2974.00 4464.00 66.6219
2005.00 8.00000 2097.00 4464.00 46.9758
2005.00 9.00000 797.000 4320.00 18.4491
2005.00 10.00000 0.000000 0.000000 0.000000
2005.00 11.00000 0.000000 0.000000 0.000000
2005.00 12.0000 191.000 4464.00 4.27867
Thank you in advance.
  3 Comments
Dyuman Joshi
Dyuman Joshi on 23 Sep 2022
In this case, all empty rows are sent to the bottom, however in the earlier example there was still a row after them.
Please clarify this ambiguity and say what exactly do you want to do with the empty rows.

Sign in to comment.

Accepted Answer

dpb
dpb on 23 Sep 2022
Edited: dpb on 23 Sep 2022
A=[
2005.00 1.00000 0.000000 0.000000 0.000000
2005.00 2.00000 0.000000 0.000000 0.000000
2005.00 3.00000 0.000000 0.000000 0.000000
2005.00 4.00000 0.000000 0.000000 0.000000
2005.00 5.00000 0.000000 0.000000 0.000000
2005.00 6.00000 0.000000 0.000000 0.000000
2005.00 5.00000 56.0000 4464.00 1.25448
2005.00 6.00000 2937.00 4320.00 67.9861
2005.00 7.00000 2974.00 4464.00 66.6219
2005.00 8.00000 2097.00 4464.00 46.9758
2005.00 9.00000 797.000 4320.00 18.4491
2005.00 12.0000 191.000 4464.00 4.27867];
% engine
d=setdiff([1:size(A,1)].',A(:,2));
n=histc(A(:,2),unique(A(:,2)));
A(n>1,2)=d;
A=sortrows(A,[1:2]);
format bank % to prettify output only
disp(A)
2005.00 1.00 0 0 0 2005.00 2.00 0 0 0 2005.00 3.00 0 0 0 2005.00 4.00 0 0 0 2005.00 5.00 56.00 4464.00 1.25 2005.00 6.00 2937.00 4320.00 67.99 2005.00 7.00 2974.00 4464.00 66.62 2005.00 8.00 2097.00 4464.00 46.98 2005.00 9.00 797.00 4320.00 18.45 2005.00 10.00 0 0 0 2005.00 11.00 0 0 0 2005.00 12.00 191.00 4464.00 4.28
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays 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!