How to have random groups( of 2 elements) from a squared matrix?

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. (for other examples see comment below)
thank you.

댓글 수: 3

What is n? can you give one of expected result?
Lorenz, if you read your post from the perspective of someone who has no idea of what you want, you might realize that you have neglected to give anywhere near enough detail. Please, don't make us guess at what you really want.
I apologize. For example the hereover matrix A. The "function" has to generate:
V=[1 4;4 3;2 1;...] %here are the groups from the matrix A. Total 8 groups in the matrix V.
As you can see, V is composed of V=[A(1,1) A(1,2); A(2,1) A(3,1);...]
And these elements are adjacent in A.
But it can be an other order (random).
for example: V=[1 4;3 2;...]. here V is composed by V=[A(1,1) A(2,1); A(3,1) A(4,1);...] And of course total 8 groups.
Do you see what I mean?

댓글을 달려면 로그인하십시오.

답변 (1개)

Doug Hull
Doug Hull 2013년 6월 10일

0 개 추천

There are 24 four connected pairs. Three per row, three per column.
I would generate a random integer from 1-numel(A) I would generate a random direction from 1-4 (North East South West) If the starting point and direction bring you out of the matrix. Draw again.
If it is a valid pairing, subset out what you are looking for using any indexing method you prefer.
Doug

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2013년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by