Change rows in a matrix so that no number is repeated more than 3 times in each column
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Matt Kindig
2012-4-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!