Need programming help

Trying to 'randomly pick a word' by using Matlab. I set up a column with numerical values 1-4, then I chose a random number generator to pick a number. But now, (the part I need help with) I am trying to set the numerical value to a word associated with the number, I am new at this; please help if you can.
k=0;
cols=['WORD'];
WORD=0;
for i=1
for j=1:4
k=k+1;
word(j,i)=k;
used(j,i)=0;
end
end
count=0;
gameplay=1;
while gameplay
pick=0;
while pick==0
pick_word=ceil(rand()*4);
wordplayed=word(pick_word);
if pick_word==1
wo=disp('skate');
elseif pick_word==2
wo=disp('place');
elseif pick_word==3
wo=disp('sleep');
elseif pick_work==4
wo=disp('lefty');
end
if used(pick_word)==0
used(pick_word)=wordplayed;
pick=1;
count=count+1;
end
end
if pick_word==1
WORD=WORD+1;
end
end

回答(2 个)

Instead of
wo = disp('skate');
you need
wo = 'skate';
You're mixing up the process of printing something in a window (which is what the disp function does) with a assigning a value to a variable.
I think you may have some other misconceptions too - for example "I am trying to set the numerical value to a word" doesn't make sense - and I think it would be a good investment of time to go over the "getting started" material in the documentation again.

1 个评论

Okay, thanks.
Let me try to make sense of my qoute...
"...I am trying to set the numerical value to a word..."
What I mean is, when the program I create, picks one of the random numbers, I want the word I have assigned to the number to be picked. (i.e. random number generates 1-the word picked should be skate; loop back to start, random number generates 3-the word picked should be sleep). Maybe I am making sense, but I will go back to the "getting started" material and see if that helps me.
Thank you again for your input.
W = {'skate','place','sleep','lefty'};
r = randperm(length(W));
W(r)

此问题已关闭。

提问:

JG
2011-11-13

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by