Marko
Marko

Reputation: 11

Can matlab colon operator be used with arithmetic operators?

Let's say I want to make a vector:

A = [4 8 16 32]

Is there any way to do this using the colon operator ? For example something like:

A = 4:(*2):32;

Upvotes: 0

Views: 91

Answers (1)

byetisener
byetisener

Reputation: 310

No, this is not possible in Matlab. You can use it like @Luis showed:

A = 2.^(2:5);

Or if you want to do this with a different function in the future:

A = [];
for n = 2:5
    A = [A n^2];
end

By changing the limits of the for loop and the n^2 part to your desired values, you can do it however you like.

Hope this helps.

Upvotes: 1

Related Questions