Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MATLAB HELP "For Loop"

Subject: MATLAB HELP "For Loop"

From: Walter Diolosa

Date: 13 Dec, 2011 15:16:09

Message: 1 of 3

Trying to create a program that asks user to input a number (integer) and then generates a list of randomly generated numbers (integers) between ‘1’ and number that user entered. Their should not be any repetition of numbers in the list.

I have this so far and I can't seem to figure out how to not repeat the #'s and to display the inputted integer.
I know I have to use a if command but don't know how to execute!
I have attempted to use if and else statements but I am stuck.
clc;
x = input('Enter a integer: ');

for x=1:x,
    k=round(rand*x);
    fprintf('%d\n',k)
end


Any Help is most appreciated.

    

Subject: MATLAB HELP "For Loop"

From: dpb

Date: 13 Dec, 2011 15:34:58

Message: 2 of 3

On 12/13/2011 9:16 AM, Walter Diolosa wrote:
> Trying to create a program that asks user to input a number (integer)
> and then generates a list of randomly generated numbers (integers)
> between ‘1’ and number that user entered. Their should not be any
> repetition of numbers in the list.
> I have this so far and I can't seem to figure out how to not repeat the
> #'s and to display the inputted integer. I know I have to use a if
> command but don't know how to execute!
> I have attempted to use if and else statements but I am stuck.
> clc;

x = input('Enter an integer: '); % note grammatical pedantic change :)
>
...

> Any Help is most appreciated.

doc randperm

PS To use an if...else here you'll need to save each generated value
and compare or generate N values and use unique() or one of its friends
and then generate some additional replacement values.

All in all, unless it is a class assignment and randperm is verboten
it's much the simpler solution (besides, "it's the Matlab way" :) ).

--

Subject: MATLAB HELP "For Loop"

From: Baalzamon

Date: 13 Dec, 2011 21:39:08

Message: 3 of 3

Looks like the aether stole my post...

Another method would be randi function.

myarray = randi(user_inputted_number)

If statements to avoid zero or letters from user etc...

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us