thepro22
thepro22

Reputation: 169

How do I create a 1 by N matrix, with elements from 1 to N (matlab)?

quick beginner MATLAB question. How do I create a 1 by N matrix with elements going from 1 to N?

Ex. [1 2 3 4 ..... N]

thanks!

Upvotes: 1

Views: 7787

Answers (1)

PengOne
PengOne

Reputation: 48398

Just do

[1:N];

The brackets are optional.

There is also another option if you want to change the increment to something other than 1. The general pattern is

[ start : step : stop ];

So if you want only even numbers from 2 to 100, you can do

[2:2:100];

Or if you want to get numbers from 1 to 0 decrementing by .1 you can do

[1:-0.1:0];

I highly recommend you take a quick squiz through the Matlab Getting Started Guide. It covers the basics such as this.

Upvotes: 6

Related Questions