Assigning random numbers to multiple vectors

Hello,
Please allow me to preface this question with an apology as I am a novice programmer and novice MATLAB user. I am trying to assign the uniformly distributed random numbers of weightEggs to the four vectors listed (sEggs, mEggs, lEggs, and xlEggs) to no avail. Can someone please tell me what I am doing incorrectly? My code is below. Thank you!
%Create a vector called weightEggs of size 1x100 containing random numbers
%(taken from the uniform distribution) between 45 and 85.
weightEggs = randi([45 85],1,100);
sEggs = weightEggs <= 53
mEggs = (weightEggs > 53) | (weightEggs <= 63)
lEggs = (weightEggs > 63) | (weightEggs <= 73)
xlEggs = weightEggs > 73
if weightEggs <= 53
disp([ num2str(weightEggs) ' are small eggs.' ])
weightEggs = sEggs;
elseif weightEggs > 53 | weightEggs <= 63
disp([ num2str(weightEggs) ' are medium eggs.' ])
weightEggs = mEggs;
elseif weightEggs > 63 | weightEggs <= 73
disp([ num2str(weightEggs) ' are large eggs.' ])
weightEggs = lEggs;
elseif weightEggs > 73
disp([ num2str(xlEggs) ' are extra-large eggs.' ])
weightEggs = xlEggs;
end

 채택된 답변

Star Strider
Star Strider 2019년 10월 20일

0 개 추천

I am not certain what you are doing, or what the requirements of what is probably a homework problem are.
First, ‘sEggs’ and the others that you calculate in the beginning are vectors of logical subscripts, so you need to use them as such.
Second, if-else statements work such that the first statement that is true will then do what that condition requires and then exit the if block. See the Description section of the if,elseif,else documentation.
I changed your code slightly to make it do what I believe you want it to do:
weightEggs = randi([45 85],1,100);
sEggs = weightEggs <= 53;
mEggs = (weightEggs > 53) & (weightEggs <= 63);
lEggs = (weightEggs > 63) & (weightEggs <= 73);
xlEggs = weightEggs > 73;
if any(weightEggs <= 53)
disp([ num2str(weightEggs(sEggs)) ' are small eggs.' ])
% weightEggs = sEggs;
end
if any(weightEggs > 53 | weightEggs <= 63)
disp([ num2str(weightEggs(mEggs)) ' are medium eggs.' ])
% weightEggs = mEggs;
end
if any(weightEggs > 63 | weightEggs <= 73)
disp([ num2str(weightEggs(lEggs)) ' are large eggs.' ])
% weightEggs = lEggs;
end
if any(weightEggs > 73)
disp([ num2str(weightEggs(xlEggs)) ' are extra-large eggs.' ])
% weightEggs = xlEggs;
end
Also see the documentation for the any function.
Make appropriate changes to get the result you want.

댓글 수: 4

NewtoMATLAB
NewtoMATLAB 2019년 10월 20일
편집: NewtoMATLAB 2019년 10월 20일
Thank you! That is my mistake for not being clear. Once the uniformly distributed random numbers of weightEggs are generated, I wanted to assign all of the elements to the four vectors: sEggs, mEggs, lEggs, and xlEggs.
What is being assigned to sEggs, mEggs, lEggs, and xlEggs with my code, however, are just 0's and 1's, not the uniformly distributed random numbers of weightEggs. Maybe a For loop would be more appropriate?
I was actually experimenting with variants that included the any function, so at least I know I am not completely off-base. :)
My pleasure!
The 0’s and 1’s are elements of logical vectors. So while:
sEggs = weightEggs <= 53;
creates a logical vector ‘sEggs’, the random numbers you want are:
weightEggs(sEggs)
(whatever variable you want to assign it to) that will give you the subset of ‘weightEggs’ that correspond to the criteria that created ‘sEggs’. The same is true for the others. Also, you do not need the find function. The logical indexing approach works here.
You also do not need any of the if blocks, since they are actually not doing anything. However I did not know if they were part of the assignment, so I kept them in.
NewtoMATLAB
NewtoMATLAB 2019년 10월 20일
편집: NewtoMATLAB 2019년 10월 20일
Thank you so much, Star Strider! Just want to express my sincerest gratitude for your time and patience in helping me work through things. I see precisely what you mean. I am not proud to share the following because I know it is the opposite of elegantly written code (haha), but this is what I came up with to satisfy the requirements for the given scenario:
%Create a vector called weightEggs of size 1x100 containing random numbers
%(taken from the uniform distribution) between 45 and 85.
weightEggs = randi([45 85],1,100);
sEggs = [weightEggs <= 53];
mEggs = [(weightEggs > 53) & (weightEggs <= 63)];
lEggs = [(weightEggs > 63) & (weightEggs <= 73)];
xlEggs = [weightEggs > 73];
if any(weightEggs <= 53)
disp(['The weights of the small eggs are: ' num2str(weightEggs(sEggs))])
if any(weightEggs > 53 | weightEggs <= 63)
disp(['The weights of the medium eggs are: ' num2str(weightEggs(mEggs))])
end
if any(weightEggs > 63 | weightEggs <= 73)
disp(['The weights of the large egss are: ' num2str(weightEggs(lEggs))])
end
if any(weightEggs > 73)
disp(['The weights of the extra-large eggs: ' num2str(weightEggs(xlEggs))])
end
end
Star Strider
Star Strider 2019년 10월 21일
As always, my pleasure!
No worries! Good, efficient coding comes with practice. The most important parameter is wanting to learn.

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

추가 답변 (0개)

질문:

2019년 10월 20일

댓글:

2019년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by