How can I remove values from an array
조회 수: 1 (최근 30일)
이전 댓글 표시
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);
답변 (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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 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!