How can I remove values from an array
显示 更早的评论
Im having issues with removing values that can be inputted by the user in my matlab program. I have 6 randomly generated vales show up and im supposed to be able to remove any matching set of numbers from the array however im having issues getting these numbers removed then displaying the updated array again. Is there any way you can help? Any help would be appreciated.
This is what I have so far:
clear
clc
rng('shuffle')
disp('Welcome to Farkle!');
NumberOfTurns=1;
fprintf('NumberOfTurns:')
fprintf('%4.0i\n',NumberOfTurns)
TurnScore=1;
fprintf('TurnNumber:')
fprintf('%4.0i\n',TurnScore')
GameScore=0;
fprintf('GameScore:')
fprintf('%4.0i\n',GameScore)
NumberOfDice=6;
fprintf('NumberOfDice:')
fprintf('%4.0i\n', NumberOfDice)
Roll=randi(6,1,6);
fprintf('Dice Values:\n')
fprintf('%6.0f\t',Roll)
fprintf('\n')
SortRoll=sort(Roll,'descend');
fprintf('Sorted Dice Values:\n')
fprintf('%6.0f\t',SortRoll)
fprintf('\n')
%% Score Input
KeepScore=input('Enter the score of this roll:');
if KeepScore>0
RoundScore=KeepScore;
OverallScore=GameScore+RoundScore;
fprintf('Game Score:\n')
fprintf('%6.0\t',OverallScore)%Make sure to display gamescore number
KeepNumber=input('Enter the dice number that you wish to keep:');
SortRoll(KeepNumber)=[];
2 个评论
Geoff Hayes
2019-2-28
William - where in the above code do you want to remove duplicated values? From the
Roll=randi(6,1,6);
William Plummer
2019-2-28
回答(1 个)
possibility
2019-2-28
In the "Roll" vector, assume you want to remove 3 from the array.
Roll(find(Roll==3))=[];
"find" command outputs the indices of 3 within the "Roll" vector.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!