dvide 500 random color to 80 different basket

조회 수: 2 (최근 30일)
shalom kobi
shalom kobi 2021년 5월 3일
댓글: shalom kobi 2021년 5월 4일
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

채택된 답변

shalom kobi
shalom kobi 2021년 5월 4일
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

추가 답변 (1개)

KSSV
KSSV 2021년 5월 4일
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
shalom kobi
shalom kobi 2021년 5월 4일
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)
shalom kobi
shalom kobi 2021년 5월 4일
hey
i resolve the issue
you can check the code i upload

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by