MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Thread Subject: Forming groups of numbers and randomizing the order of groups

 Subject: Forming groups of numbers and randomizing the order of groups From: Ulrik Nash Date: 11 May, 2012 15:59:21 Message: 1 of 5 Hi Everyone, Suppose I have a matrix with two columns like this: A = [2 3;3 4;2 1;4 2;3 1]; From A I wish to create matrix B, which has the following features: 1) The first column in B groups the numbers in the first column of A, moving the adjacent numbers in column 2 of A correspondingly. 2) The 'groups' of numbers in B's first column are in no particular. They may start with the lowest, highest, or any in between. 3) Within each 'group' of numbers, the order is random. So, for the above example, the following could be B: B = [3 4;3 1;2 1;2 3;4 2] or B = [2 1; 2 3; 3 1; 3 4;4 2] or B = [4 2; 2 1; 2 3; 3 4; 3 1] etc. How might this be done?
 Subject: Forming groups of numbers and randomizing the order of groups From: Ben Date: 11 May, 2012 16:11:12 Message: 2 of 5 "Ulrik Nash" wrote in message ... > Hi Everyone, > > Suppose I have a matrix with two columns like this: > > A = [2 3;3 4;2 1;4 2;3 1]; > > From A I wish to create matrix B, which has the following features: > > 1) The first column in B groups the numbers in the first column of A, moving the adjacent numbers in column 2 of A correspondingly. > > 2) The 'groups' of numbers in B's first column are in no particular. They may start with the lowest, highest, or any in between. > > 3) Within each 'group' of numbers, the order is random. > > So, for the above example, the following could be B: > > B = [3 4;3 1;2 1;2 3;4 2] > > or > > B = [2 1; 2 3; 3 1; 3 4;4 2] > > or > > B = [4 2; 2 1; 2 3; 3 4; 3 1] > > etc. > > How might this be done? A = [2 3;3 4;2 1;4 2;3 1]; R = randperm(length(A)); B = zeros(size(A)); for index = 1:length(R)         B(R(index),:) = A(index,:); end
 Subject: Forming groups of numbers and randomizing the order of groups From: Ulrik Nash Date: 11 May, 2012 16:23:12 Message: 3 of 5 "Ben" wrote in message ... > "Ulrik Nash" wrote in message ... > > Hi Everyone, > > > > Suppose I have a matrix with two columns like this: > > > > A = [2 3;3 4;2 1;4 2;3 1]; > > > > From A I wish to create matrix B, which has the following features: > > > > 1) The first column in B groups the numbers in the first column of A, moving the adjacent numbers in column 2 of A correspondingly. > > > > 2) The 'groups' of numbers in B's first column are in no particular. They may start with the lowest, highest, or any in between. > > > > 3) Within each 'group' of numbers, the order is random. > > > > So, for the above example, the following could be B: > > > > B = [3 4;3 1;2 1;2 3;4 2] > > > > or > > > > B = [2 1; 2 3; 3 1; 3 4;4 2] > > > > or > > > > B = [4 2; 2 1; 2 3; 3 4; 3 1] > > > > etc. > > > > How might this be done? > > A = [2 3;3 4;2 1;4 2;3 1]; > R = randperm(length(A)); > B = zeros(size(A)); > for index = 1:length(R) > B(R(index),:) = A(index,:); > end Thanks Ben, but the numbers in the first columns should be grouped, so for example, all the 1's are grouped together, the 3's are grouped, and so on. Then these groups should be randomized in order, so that the 3's for example come first, the 1's second and so on. Meanwhile, within the groups, the order should also be randomized.
 Subject: Forming groups of numbers and randomizing the order of groups From: Ben Date: 11 May, 2012 16:45:13 Message: 4 of 5 > Thanks Ben, but the numbers in the first columns should be grouped, so for example, all the 1's are grouped together, the 3's are grouped, and so on. Then these groups should be randomized in order, so that the 3's for example come first, the 1's second and so on. Meanwhile, within the groups, the order should also be randomized. Ahh, well if you play around with the same idea and use find, sort, and unique, you should be able to get it worked out. There are two approaches that come to mind: 1) Group the matrices the way you want (read documentation on find to help) and split them into different matrices based on your grouping requirements. Then randomly put them back together. (This is how I would approach this problem.) 2) Unique with the rows argument specified will return a matrix sorted by row-- that will group all of your data. Then you can add those rows that match a value in a loop as done in my first answer. A helpful two lines: T = unique(A,'rows'); find(T(:,1)==4) Capture the output of the find line and you will have the index of all the rows that have a 4 in the first column. Armed with that information, you can loop through and add them like I did in the earlier (simpler) example -- though it will take some thought on your part to figure it out. Give it a shot and come back if it's still murky -- and post the code of your attempt.
 Subject: Forming groups of numbers and randomizing the order of groups From: Ben Date: 11 May, 2012 16:56:13 Message: 5 of 5 "Ben" wrote in message ... > > > Thanks Ben, but the numbers in the first columns should be grouped, so for example, all the 1's are grouped together, the 3's are grouped, and so on. Then these groups should be randomized in order, so that the 3's for example come first, the 1's second and so on. Meanwhile, within the groups, the order should also be randomized. > > Ahh, well if you play around with the same idea and use find, sort, and unique, you should be able to get it worked out. > > There are two approaches that come to mind: > 1) Group the matrices the way you want (read documentation on find to help) and split them into different matrices based on your grouping requirements. Then randomly put them back together. (This is NOT how I would approach this problem.) > 2) Unique with the rows argument specified will return a matrix sorted by row-- that will group all of your data. Then you can add those rows that match a value in a loop as done in my first answer. A helpful two lines: > T = unique(A,'rows'); > find(T(:,1)==4) > > Capture the output of the find line and you will have the index of all the rows that have a 4 in the first column. Armed with that information, you can loop through and add them like I did in the earlier (simpler) example -- though it will take some thought on your part to figure it out. Give it a shot and come back if it's still murky -- and post the code of your attempt. ...forgot the all important NOT up in step 1. I would NOT approach the problem that way. I recommend 2.