dvide 500 random color to 80 different basket

hey
im trying to make a code that recive 500 ball ( 100 of each color : red blue yellow green purple)
then i want matlab will dvide this 500 ball randomly to 80 different basket
i have tryd few command and loops without succses
here is my code so far
%%
clear all
clc
for i=1:100
%deck(i,:) =( ["1","2","3","4","5"]' ); test
deck(i,:) =( ["R","G","P","Y","B"]' );
i=i+5;
end
n=0; % counter
%r=randi([1,80],1,500);
for i=1:500
temp=(randi([1,80]));
n=n+1;
K(n,:)=temp;
end
% not sure if nessacery
A=zeros (500,80);
B=zeros (500,80);
%dvide the 500 ball to 80 basket
for i=1:500
temp=deck(i);
tempb=K(i);
A(tempb,:)=temp;
B(:,tempb)=temp;
C(:,K(i))=deck(i);
D(K(i,:))=deck(i);
%F=struct(tempb,temp);
%data.K(i,:)=deck(i);
%data(K(i,:)).tempb=deck(i);
random(K(i,:)).tempb=deck(i);
end
i will appriciate any help to solve this problem

 Risposta accettata

shalom kobi
shalom kobi il 4 Mag 2021
clear all
clc
% define my deck 100 ball each color
for i=1:100
deck_num(i,:) =( [1,2,3,4,5]'); % test
i=i+5;
end
%dvide to 80 basket for 500 balls
r=randi([1,80],1,500);
for i=1:500
x=(r(i)); % go to number of busket
deck_b(i,r(i))=deck_num(i);
end
% count how many are in the basket from each color
for i=1:80
s1=numel(find(deck_b(:,i)==1)); % count the red color
s2=numel(find(deck_b(:,i)==2)); % count the green color
s3=numel(find(deck_b(:,i)==3)); % count the purple color
s4=numel(find(deck_b(:,i)==4)); % count the yellow color
s5=numel(find(deck_b(:,i)==5)); % count the blue color
basket(:,i)=[s1 s2 s3 s4 s5]; % final result culom 1:80 are basket and row 1:5 (vector) define 5 different color
end

Più risposte (1)

N = 100 ;
R = ones(N,1) ;
B = 2*ones(N,1) ;
Y = 3*ones(N,1) ;
G = 4*ones(N,1) ;
P = 5*ones(N,1) ;
balls = [R; B; Y; G; P] ;
% randomise the order
balls = balls(randperm(length(balls)))
% choose any 80 balls out of it
iwant = balls(randperm(length(balls),80))

4 Commenti

shalom kobi
shalom kobi il 4 Mag 2021
Modificato: shalom kobi il 4 Mag 2021
thanks for your answer
this solution is not working for me since i want to dvide all 500 balls
the final data i want is : example
busket no 1 : 20 red 30 yellow 50 purple
busket no 2 ; 40 red 50
basket no 80 : 80 green
i also allready improve my code and make it much shorter but still i got issue to get the finel result i want
my final result deck_num give me a lot of missong array which i dont know how to delete them
here is my new code :
clear all
clc
r=randi([1,80],1,500);
for i=1:100
deck_num(i,:) =( ["1","2","3","4","5"]' );
%deck_RGB(i,:) =( ["R","G","P","Y","B"]' ); % test
i=i+5;
end
for i=1:500
x=(r(i)); % go to number of busket
deck_b(i,r(i))=deck_num(i);
end
You already know what should be there in each basket...... what is there to solve now and code?
no i dont know what should be in each busket
its an experiment and each time i run the code i exept to different soulotion ( randomly)
hey
i resolve the issue
you can check the code i upload

Accedi per commentare.

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Prodotti

Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by