How can I remove values from an array
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Geoff Hayes
am 28 Feb. 2019
William - where in the above code do you want to remove duplicated values? From the
Roll=randi(6,1,6);
Antworten (1)
possibility
am 28 Feb. 2019
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 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!