Change rows in a matrix so that no number is repeated more than 3 times in each column
2 views (last 30 days)
Show older comments
Hi there,
Currently I am using the script below to change the order of rows in a matrix such that no number is repeated consecutively in either the first or second column. How do I change the script such that numbers can be repeated twice or three times, but no more?
Many thanks,
James
function d=repcheck(b,s)
%The input b is a matrix
%The input s is the number of rows
e=0; for c=1:s d(c,:)=b(1+e,:); b(1+e,:)=[];e=0;
while((isempty(b)) || (d(c,1)==b(1+e,1))||(d(c,2)==b(1+e,2)));
e=e+1;
if ((e>=(numel(b(:,1))-1)) || (numel(b(:,1))<2))
d=[d;b];
return
end
end
end
0 Comments
Answers (2)
Matt Kindig
on 18 Apr 2012
I'm not sure what you are trying to do. I tested this script with the input matrix:
8 8 91
9 9 18
91 77 26
15 15 15
83 87 14
54 8 87
100 100 58
8 26 55
44 80 14
11 43 85
and it just returned out my original matrix. Can you post an example matrix of what you are trying to do?
0 Comments
See Also
Categories
Find more on Creating and Concatenating 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!