How can I remove values from an array

조회 수: 3 (최근 30일)
William Plummer
William Plummer 2019년 2월 28일
답변: possibility 2019년 2월 28일
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
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);
You could perhaps use unique...
William Plummer
William Plummer 2019년 2월 28일
yes my randi function generates 6 random numbers ( 1,3,3,5,2,3 for example) I need help removing certain values from the array (such as the number 1).

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

답변 (1개)

possibility
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by