フィルターのクリア

How randomly choose pairs of elements from a squared matrix and put them in an other matrix?

1 回表示 (過去 30 日間)
Hi, I have a matrix A
A =
1 4 2 3
4 3 1 2
3 2 4 1
2 1 3 4
How to make different groups of two elements from this matrix? But the chosen elements (from the matrix A) have to be adjacent in the matrix A. for example A(1,1) & A(1,2) are adjacent and so they can be chosen. and A(1,1) & A(4,3) can't be chosen to make a group. I'd like to create a function which randomly choose these elements from A. And the function generates an other matrix containing the (2 by 2) different groupements randomly created.
example "function group":
V=group(A)
V= 1 4
3 2
4 2
3 1
2 1
4 3
3 2
1 4
This new V matrix is made of groups
A(1,1)&A(2,1)
A(2,2)&A(4,1)
A(1,2)&A(1,3)
...
Hope you understand what I mean. In fact we randomly choose pairs of elements in the matrix A. thank you!

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 11 日
編集済み: Azzi Abdelmalek 2013 年 6 月 11 日
m=5
n=numel(A)
ii=0
while ii<m
id=randperm(16);
[ii1,jj1]=ind2sub(size(A),id(1));
[ii2,jj2]=ind2sub(size(A),id(2));
if and(abs(ii1-ii2)<=1,abs(jj1-jj2)<=1);
ii=ii+1;
B(ii,:)=[A(id(1)) A(id(2))]
end
end
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 11 日
編集済み: Azzi Abdelmalek 2013 年 6 月 11 日
m=5
n=numel(A)
ii=0
while ii<m
id=randperm(16);
[ii1,jj1]=ind2sub(size(A),id(1));
[ii2,jj2]=ind2sub(size(A),id(2));
if and(abs(ii1-ii2)<=1,abs(jj1-jj2)<=1);
ii=ii+1;
B(ii,:)=[A(id(1)) A(id(2))]
indice1{ii,1}=[ii1 jj1]
indice2{ii,1}=[ii2 jj2]
end
end
[cell2mat(indice1) cell2mat(indice2)]
Lorenz
Lorenz 2013 年 6 月 11 日
THANK YOu very much. But I am sorry, there is just one problem I just forgot to mentionate. The chosen elements must be adjacent but not "diagonnaly adjacent", and they must be chosen just ONE TIME (the chosen elements can't be REchoose). So, at the end, I have to get only 8 pairs.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 11 日
編集済み: Azzi Abdelmalek 2013 年 6 月 11 日
m=5
n=numel(A)
ii=0
idx=[];
while ii<m
id=setdiff(randperm(16),idx,'stable');
[ii1,jj1]=ind2sub(size(A),id(1));
[ii2,jj2]=ind2sub(size(A),id(2));
if or(abs(ii1-ii2)==1 & abs(jj1-jj2)==0,abs(ii1-ii2)==0 & abs(jj1-jj2)==1);
ii=ii+1;
idx=[idx id(1:2)];
B(ii,:)=[A(id(1)) A(id(2))]
end
end
disp(idx) % Linear index
[idx1,idx2]=ind2sub(size(A),idx);
  4 件のコメント
Lorenz
Lorenz 2013 年 6 月 11 日
編集済み: Lorenz 2013 年 6 月 11 日
Ok Thanks. If I put m=8, matlab run into a very long loop (Busy), is that normal? In fact, there is one only point that the script can't associate with an adjacent point, because these other points are already chosen. It is not a big problem but that's a pitty. I'll try to correct it. please give me idea if you have. Thank you very much
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 11 日
That means it can not find 8 couples of adjacent elements

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

カテゴリ

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