필터 지우기
필터 지우기

create a randomised vector from matrix

조회 수: 1 (최근 30일)
Darya Frank
Darya Frank 2016년 7월 30일
댓글: Azzi Abdelmalek 2016년 7월 31일
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!

답변 (2개)

Image Analyst
Image Analyst 2016년 7월 30일
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
Image Analyst
Image Analyst 2016년 7월 30일
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 2016년 7월 31일
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?

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


Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 30일
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
Image Analyst
Image Analyst 2016년 7월 31일
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.
Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 31일
You are right

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

카테고리

Help CenterFile 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