hello,
how do I make sure to find the next number greater and less than the number requested from the user, but it must contain the same digits as the requested number
for exemple: you get 536 so the answer will be 365 and 635

 채택된 답변

Image Analyst
Image Analyst 2020년 11월 15일

0 개 추천

Try this:
% Create original number
nUser = 536;
% Convert to string.
strx = num2str(nUser);
% Get all possible permutations of the digits.
p = perms(1 : length(strx))
% Make list of all possible combinations
counter = 1;
for k = 1 : size(p, 1)
nAll(k) = str2double(strx(p(k,:)));
end
% Sort them
nSorted = sort(nAll, 'ascend');
% Find the index of the original number
index = find(nSorted == nUser)
% Get the two numbers that are just below and just above that number.
n = [nSorted(index-1), nSorted(index+1)];
% All Done! Display all the numbers in the command window:
nAll
n
You'll see
p =
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
index =
3
nAll =
635 653 365 356 563 536
n =
365 563
n is a list of all possible numbers that can be created from your original digits.
Be aware that you'll have to do some validation to see if the user's number is the smallest or largest possible because there would be no lower or higher number in those cases.
Is this what you were looking for?

댓글 수: 5

hello,
thanks that was what I was looking for but how can I make sure that some numbers don't repeat
for exemple 366 because there are two 6 each number will repeat 2 times
Check the input string for that before you get very far:
% Create original number
nUser = 366;
% Convert to string.
strx = num2str(nUser);
% See if any digits repeat
for k = 1 : length(strx)
numOccurrences = sum(strx == strx(k));
if numOccurrences > 1
% There is a repeat. Warn the user.
errorMessage = sprintf('Error : %s occurs %d times in %s.\nEach digit must appear only once',...
strx(k), numOccurrences, strx);
uiwait(errordlg(errorMessage));
% Bail out of the program.
return;
end
end
% Get all possible permutations of the digits.
p = perms(1 : length(strx))
% Make list of all possible combinations
counter = 1;
for k = 1 : size(p, 1)
nAll(k) = str2double(strx(p(k,:)));
end
% Sort them
nSorted = sort(nAll, 'ascend');
% Find the index of the original number
index = find(nSorted == nUser)
% Get the two numbers that are just below and just above that number.
n = [nSorted(index-1), nSorted(index+1)];
% All Done! Display all the numbers in the command window:
nAll
n
thanks but now you get a error message when a number is double but is therre a possibillty to just don't hav the numbers double
for exemple the number 366
366
366
636
636
663
663
but I just need it like this:
366
636
663
Thanks in advance!
Image Analyst
Image Analyst 2020년 12월 12일
편집: Image Analyst 2020년 12월 12일
After you get nAll, pass it through unique():
nAll = unique(nAll);
thanks!!!

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

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 15일

0 개 추천

Try this
x = 536;
y = num2str(x);
idx1 = randi([2 numel(y)]); % to make sure we don't get same number
idx = [idx1 setdiff(randperm(numel(y)), idx1)];
y = str2double(y(idx));

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2020년 11월 15일

댓글:

2020년 12월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by