i have a martix A, i want missing numbers in the 2nd column as i explained? any help??
1 view (last 30 days)
Show older comments
M.Prasanna kumar
on 5 Feb 2019
Commented: M.Prasanna kumar
on 5 Feb 2019
A =
1 3
1 5
1 6
1 8
1 10
2 4
2 8
2 9
3 1
3 3
3 5
3 7
3 9
i want output as ROWS starting with 1 = missing numbers are 2 ,4,7,9
ROWS starting with 2 = missing numbers are 1,2 ,3,5,6,7,10
ROWS starting with 1 = missing numbers are 2,4,6,8,10
0 Comments
Accepted Answer
madhan ravi
on 5 Feb 2019
Alternative:
T=array2table(A);
fun=@(x) setdiff(min(A(:,1)):max(A(:,2)),x);
C=rowfun(fun,T,...
'GroupingVariable','A1',...
'OutputFormat','cell');
Result=[repelem(unique(A(:,1)),cellfun(@length,C)) [C{:}]']
More Answers (2)
TADA
on 5 Feb 2019
Edited: TADA
on 5 Feb 2019
granted you always have 1:3 in the first column and values between 1 and 10 in the second:
A = [1,3; 1,5; 1,6; 1,8; 1,10; 2,4; 2,8; 2,9; 3,1; 3,3; 3,5; 3,7; 3,9]; % this is your matrix
B = [repmat([1;2;3],10,1) repmat((1:10)', 3, 1)];
C = setdiff(B,A,'rows');
This is the output:
C =
1 1
1 2
1 4
1 7
1 9
2 1
2 2
2 3
2 5
2 6
2 7
2 10
3 2
3 4
3 6
3 8
3 10
madhan ravi
on 5 Feb 2019
Edited: madhan ravi
on 5 Feb 2019
The below works for n numbers in the first column:
Min=min(A(:,2));
Max=max(A(:,2));
[a,b,c]=unique(A(:,1));
B=diff(b.');
C=mat2cell(A(:,2),[B numel(c)-sum(B)],1);
R=cellfun(@(x) setdiff(Min:Max,x),C,'un',0);
Result=[repelem(a,cellfun(@length,R)) [R{:}]']
Gives:
Result =
1 1
1 2
1 4
1 7
1 9
2 1
2 2
2 3
2 5
2 6
2 7
2 10
3 2
3 4
3 6
3 8
3 10
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!