Just a small problem

Hi! Can you help me please with this problem: How can I Write a function that produces random integers From 1000 to 9999, and including the numbers of different 4-digit (with no repetition)? In MatLab...

2 件のコメント

Andrew Newell
Andrew Newell 2011 年 12 月 30 日
For a start, see randint: http://www.mathworks.com/help/toolbox/comm/ref/randint.html.
Izmir
Izmir 2011 年 12 月 30 日
I need the program as soon as possible.... Please help me????? :(

サインインしてコメントする。

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 12 月 30 日

1 投票

function y=MyRandInt
while true
y=floor(8999*rand+1000);
if length(unique(num2str(y)))==4
return;
end
end

11 件のコメント

Izmir
Izmir 2011 年 12 月 30 日
Thanks very Much!!!!!
Izmir
Izmir 2011 年 12 月 30 日
How to save the .m file to bring it back by a command?
Fangjun Jiang
Fangjun Jiang 2011 年 12 月 30 日
Save the above code into MyRandInt.m and put it in your current folder or any where in the MATLAB path, then run MyRandInt at the Command Window.
Izmir
Izmir 2011 年 12 月 30 日
Ok. Thanks!!! :)
Izmir
Izmir 2011 年 12 月 30 日
Is it possible to write the different program with the same function?
Izmir
Izmir 2011 年 12 月 30 日
Can you also comment the lines of the program?
function y=MyRandInt
while true
y=floor(8999*rand+1000);
if length(unique(num2str(y)))==4
return;
end
end
Walter Roberson
Walter Roberson 2011 年 12 月 30 日
Do you need the functionality "as soon as possible", or do you need someone to complete an assignment for you "as soon as possible"?
If you need the functionality, you have it. If this is an assignment, then you should write your own code -- or at the very least do your own analysis of how the code works.
Fangjun Jiang
Fangjun Jiang 2011 年 12 月 30 日
If you don't understand any function or keywords, such as while, true, floor, rand, length, unique, num2str, return, etc. type the following to get the help document
help while
doc while
help floor
doc floor
...
Izmir
Izmir 2011 年 12 月 30 日
I made it. Now no problem... I'm beginner in MatLab, that's why...
Jan
Jan 2011 年 12 月 30 日
Even, or especially, a beginner has to read the Getting Started chapters of the documentation. You cannot use such a powerful language like Matlab without spending the time to learn the basics.
Fangjun Jiang
Fangjun Jiang 2011 年 12 月 31 日
It took me more than 30 minutes to come up with the answer. 20 minutes for thinking about whether it is a homework assignment. At the end, I think it is not. 10 minutes for thinking about which approach is better, the permutation method or the try-and-error method.
I would not think this is a homework assignment for an entry level MATLAB class. Think about it, what aspect of the language (programming flow, for-loops, or if-else statment) would be a good fit to solve this problem?
Jan has been an A+ student here. But I would grade all three of his homework a B+. Which entry level class or beginner student would use randperm() or perms()? I would expect brutal force if b(4)~=b(3) && b(4)~=b(2) && b(4)~=b(1) ... etc.

サインインしてコメントする。

その他の回答 (3 件)

Jan
Jan 2011 年 12 月 31 日

2 投票

Finite and no large table:
a = 0:9;
i1 = ceil(rand * 9);
a(i1 + 1) = [];
p = randperm(9);
result = i1 * 1000 + a(p(1:3)) * [100; 10; 1]

2 件のコメント

Fangjun Jiang
Fangjun Jiang 2011 年 12 月 31 日
B+
Daniel Shub
Daniel Shub 2011 年 12 月 31 日
This is how I would have done it.

サインインしてコメントする。

Jan
Jan 2011 年 12 月 30 日

1 投票

It really looks like a homework and your teacher/professor will not recommend this forum to other students any longer after your homework has been solved here. Be sure to mention the assistence you got, otherwise it would be cheating.
I'd prefer a solution, which is guaranteed to stop in finite time. But I have only this idea:
a = 0:9;
while true
b = a(randperm(10, 4)); % Matlab 2011b required
if b(1) ~= 0
break;
end
end
result = b * [1000; 100; 10; 1]
For earlier Matlab versions:
b = a(randperm(10));
...
result = b(1:4) * [1000; 100; 10; 1]
Jan
Jan 2011 年 12 月 30 日

1 投票

An algorithm in finite time, but not really efficient:
K = perms(0:9);
K(K(:,1) == 0, :) = [];
index = ceil(rand * size(K, 1));
result = K(index, 1:4) * [1000; 100; 10; 1]
It is more efficient to create a [Nx4] table only instead of the [Nx10] table of perms:
K = VChooseKO(0:9, 4);
See: FEX: VChooseKO. But even then the large table is brute.

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by