Change rows in a matrix so that no number is repeated more than 3 times in each column

2 ビュー (過去 30 日間)
J Richards
J Richards 2012 年 4 月 18 日
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

回答 (2 件)

Matt Kindig
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?

J Richards
J Richards 2012 年 4 月 19 日
Happy to clarify. My matrix looks more like this:
1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 2 1 1 2 1 2 2 1 3 2 2 1 ... etc.
The script shuffles the rows so that no column has two identical numbers one after the other. E.g. it becomes:
1 1 1 2 3 2 1 2 3 3 1 2 1 3 1 2 1 2
etc.
I'd like to change it so that numbers can be repeated, but no more than three times consecutively.
NB elements within each row are not shuffled, it's just the order of the rows which is changed.
James
  1 件のコメント
J Richards
J Richards 2012 年 4 月 19 日
oops, that didn't format right. The first matrix should be
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
etc
And the second should be
1 1 1
2 3 2
1 2 3
3 1 2
1 3 1
etc.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by