create a randomised vector from matrix

1 view (last 30 days)
Darya Frank
Darya Frank on 30 Jul 2016
Commented: Azzi Abdelmalek on 31 Jul 2016
I'm relatively new to Matlab so this might be a very simple problem to solve: I have a 37 x 8 matrix (ascending order numbers, 1:296) I need to create a 296 x 1 vector in which each row will take a number from the matrix rows, with the exception that it cannot be the same row as the previous 8 rows.
Any help would be much appreciated!

Answers (2)

Image Analyst
Image Analyst on 30 Jul 2016
Try this:
newVector = yourMatrix(:);
This will produce a column vector. No element of newVector will have come from the same row as any of the previous 8 elements of newVector did.
  3 Comments
Image Analyst
Image Analyst on 30 Jul 2016
I don't think it's possible to be totally random. You can use randperm(numel(yourMatrix)) to get a list of linear indexes in a random order, and then just keep trying until you get the condition where no element is in the same row as any other element within 8 elements of it, but I don't think you'll ever succeed. I believe you'll have to impose some kind of order onto it, which is what I did. Good luck trying tough. Let us know if you succeed, and post an actual example where it worked.
If your matrix were square you could arrange it in a Latin Square pattern https://en.wikipedia.org/wiki/Latin_square but I don't think there's any Latin rectangle pattern.
Darya Frank
Darya Frank on 31 Jul 2016
The way I was thinking was by some sort of elimination - so first create a random sequence of 8 rows and then for each new row randomly select a number from a row that hasn't appeared in the last 8 places - does that make sense?

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 30 Jul 2016
A=reshape(1:37*8,37,8) % Example
[n,m]=size(A)
idx=cell2mat(arrayfun(@(x) randperm(n)', 1:m,'un',0)')
idy=zeros(size(idx));
for k=1:n
ii=find(ismember(idx,k));
idy(ii)=randperm(m)';
end
ixy=sub2ind([n m],idx,idy);
out=A(ixy)
  2 Comments
Image Analyst
Image Analyst on 31 Jul 2016
I don't think this works. I added this check after the code:
% Let's see if any of the linear indexes is in the same row as any of the prior 8 indices
for k = 9:length(ixy)
% Get row of kth index
[rowk, colk] = find(A == ixy(k));
% Check prior 8 elements
for k2 = k-8 : k-1
% Get row of k2'th index.
% Find out the row in A where this number appears.
[rowk2, colk2] = find(A == ixy(k2));
% See if this row matches the row where ixy(k) lies.
if rowk2 == rowk
fprintf('Index %d and %d are both in row %d.\n', ixy(k), ixy(k2), rowk);
end
end
end
and it found lots of cases where an index was in the same row of the original A matrix as one of the prior 8 indexes.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!