フィルターのクリア

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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by