How to get all possible arrays by randomizing in each cells with four items (1x6 array)

조회 수: 1 (최근 30일)
Hi! I would like to know all possible arrays for 1x6 vector. Each cells may be 0 (or) x (or) y (or) z. Some can be repeated and some may exclude in an array.
Some of the results should be like below:
[x,y,x,0,z]; [x,x,x,x,z]; [y,0,0,0,0,z]; [z,0,y,0,0,x];......
Thank you so much.

채택된 답변

madhan ravi
madhan ravi 2018년 12월 29일
편집: madhan ravi 2018년 12월 29일
Use random indexing by using randi if you want the elements to repeat or randperm if you don’t want the elements to be repeated
c=cell(1,4); % preallocate
syms x y z % define x y and z as you wish
a=[0,x,y,z];
for i =1:numel(c)
c{i}=a(randi(numel(a),1,6));
end
celldisp(c)
Note : 1 X 6 array means you want to fill each cell ( totally 4 cells) with 6 elements.
  댓글 수: 4
Image Analyst
Image Analyst 2018년 12월 29일
I'm not sure how using randi, or even randperm, will get you all possible 4^6 combinations.
madhan ravi
madhan ravi 2018년 12월 29일
Yes your right , perhaps the OP wanted to just fill the cell arrays with those 4 elements since his quote "Yes Thank you so much... It works well." means he got what he was looking for .

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

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 12월 29일
At first I though of using perms() on your list of numbers but since that doesn't allow more output numbers than input numbers (6 output with only 4 input), I thought that a 6-nested for loop would work. This code figures out all possible permutations of the 4 variables taken 6 per draw. You will of course have 4^6 possible unique combinations.
tic
x = 99;
y = 188;
z = 277; % Whatever numbers you want...
listOfNumbers = [0, x, y, z]
c = zeros(4^6, 6); % Preallocate
% Generate all possible combinations.
row = 1;
for k1 = 1 : length(listOfNumbers)
for k2 = 1 : length(listOfNumbers)
for k3 = 1 : length(listOfNumbers)
for k4 = 1 : length(listOfNumbers)
for k5 = 1 : length(listOfNumbers)
for k6 = 1 : length(listOfNumbers)
c(row, :) = [listOfNumbers(k1), ...
listOfNumbers(k2), ...
listOfNumbers(k3), ...
listOfNumbers(k4), ...
listOfNumbers(k5), ...
listOfNumbers(k6)];
row = row + 1;
end
end
end
end
end
end
toc
Time to run is a speedy 0.001 seconds (a millsecond), so don't be afraid of the for loops.
  댓글 수: 2
San May
San May 2018년 12월 29일
wow incredible! Thank you so much.. It is so fast and got all possible arrays!
madhan ravi
madhan ravi 2018년 12월 29일
편집: madhan ravi 2018년 12월 29일
+1 @sir Image Analyst , always precise!

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


Stephen23
Stephen23 2018년 12월 30일
편집: Stephen23 2018년 12월 30일
A simple solution with ndgrid:
>> x = 99;
>> y = 188;
>> z = 277;
>> C = cell(1,6);
>> [C{:}] = ndgrid([0,x,y,z]);
>> C = cellfun(@(a)a(:),C,'uni',0);
>> M = [C{:}]
M =
0 0 0 0 0 0
99 0 0 0 0 0
188 0 0 0 0 0
277 0 0 0 0 0
0 99 0 0 0 0
99 99 0 0 0 0
188 99 0 0 0 0
277 99 0 0 0 0
0 188 0 0 0 0
99 188 0 0 0 0
... lots of lines here
99 99 277 277 277 277
188 99 277 277 277 277
277 99 277 277 277 277
0 188 277 277 277 277
99 188 277 277 277 277
188 188 277 277 277 277
277 188 277 277 277 277
0 277 277 277 277 277
99 277 277 277 277 277
188 277 277 277 277 277
277 277 277 277 277 277
>> size(M)
ans =
4096 6

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by