Borrar filtros
Borrar filtros

How can I remove values from an array

1 visualización (últimos 30 días)
William Plummer
William Plummer el 28 de Feb. de 2019
Respondida: possibility el 28 de Feb. de 2019
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 comentarios
Geoff Hayes
Geoff Hayes el 28 de Feb. de 2019
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 el 28 de Feb. de 2019
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).

Iniciar sesión para comentar.

Respuestas (1)

possibility
possibility el 28 de Feb. de 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.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by