create subarray from an given array

8 ビュー (過去 30 日間)
Industrial
Industrial 2014 年 3 月 21 日
コメント済み: Azzi Abdelmalek 2014 年 3 月 22 日
To create few no. of arrays from a given long array, eg. given = [1 2 3 4 5 6 7 8 9 ....]
to create randomly [3 4 9] [1 5 8] [2 6 7] ... if size of group is given between 3 and 3 (between two nos.) by user
or [2 6 9 5] [1 3 4 7 8] etc.. if the size of group is between 4 and 5 given by user (may vary and no of group can also vary) also no number should repeat again, and should be present in array given earlier

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 21 日
編集済み: Azzi Abdelmalek 2014 年 3 月 21 日
EDIT
a=[1 2 3 4 5 6 7 8 9]
n=numel(a)
% find divisor of a
c=1:n
d = c(rem(n,c)==0)
% number of groupes
ng=d(randi(numel(d),1))
%------Length of group------------------
lg=n/ng
%-------------Result---------------------
s=randperm(n);
out=num2cell(reshape(a(s),ng,lg),2)
  6 件のコメント
Industrial
Industrial 2014 年 3 月 22 日
編集済み: Industrial 2014 年 3 月 22 日
sir i am usin R2009a version of matlab
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 22 日
Ok, try the edited answer

サインインしてコメントする。


Jos (10584)
Jos (10584) 2014 年 3 月 21 日
This problem is somewhat ill-defined. If there are multiple group sizes allowed, should all group sizes be present an equal amount of times? And things might not fit, for instance, when the number of elements in A is odd, and the AllowGroupSize is even …
Here is a suggestion:
A = 1:21
AllowedGroupSize = [3 4]
GroupSizes = repmat(AllowedGroupSize,1, numel(A) ./ sum(AllowedGroupSize))
C = mat2cell(A(randperm(numel(A))),1,GroupSizes)
  1 件のコメント
Industrial
Industrial 2014 年 3 月 22 日
Thank you for your kind consideration
But in this problem all sizes of groups might not be present in the answer. Even groups may have same sizes, but may differ when executed number of times in a loop. Actually it is to be used in a Manufacturing cell family formation problem in which a part family may have some parts and another part family may have from rest of the parts.
thanks.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by