Reshaping a matrix and binding two values together

Hello dear Matlab Community!
Since I am a total novice in Matlab I'm stuck with the following, probably easily solvable, problem.
I have a matrix in the following arrangement:
20 9
12 4
3 15
6 7
. .
. .
and so forth, the integers of each column reach from 1 to 20 and are paired with an integer from the other column. The same integers cannot be paired, so for example 14 14 would be impossible.
I want to present randomized pairings of stimuli.
So, firstly I need to rearrange my two columns to this from: 20 9 12 4 3 15 6 7...
Does anybody know how to do this?
And secondly, I want to bind every 2 integers and then shuffle them. For example, 20 and 9 are bound, 12 and 4 are bound, 3 and 15 are bound and so forth. And then shuffle them, so a new arrangement might be: 12 4 20 9 3 15...
I would very much appreciate any help!
Thanks in advance and best Regards!

回答(1 个)

iwant = reshape(randperm(20),10,2) ;

8 个评论

This gives me two ten-digit colums containing the numbers 1 to 20 and I don't see how that helps me. But thanks for the answer!
If A is your m*2 matrix
A = A' ;
iwant = A(:)'
Is this what you want?
Yes! Thank you very much! :)
This is the answer to my first question.
Do you also know how to bind 2 integers together in order to shuffle the pairings?
Anyway, many thanks for your effort!
My first answer helps for this.
A = rand(10,2) ;
A = A' ;
A = A(:)' ;
idx = reshape(randperm(length(A)),10,2) ;
A = A(idx)
I get the row I want using this "A = A' ; A = A(:)' ;"
But when I try "idx = reshape(randperm(length(A)),10,2)", I get the following error: "Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension."
idx = reshape(randperm(length(A)),[],2) ;
It seems you have more than 20 elements....
Yeah, when I do this "A = A' ; A = A(:)' ;", I have my integers listed in a 40-digit row.
So, from this:
12 20
9 4
2 17
. .
. .
to this:
12 20 9 4 2 17...
Which is exactly what I wanted!
But now, I want to bind 12 and 20 together as well as 9 and 4, 2 and 17 and so forth.
And then shuffle the row. One possible outcome would be 9 4 2 17 12 20....
Do you know, what I mean?

请先登录,再进行评论。

类别

帮助中心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!

Translated by