2 views (last 30 days)

I have

x=0:0.01:1

then I want to create a vector y that corresponds to each value of x

ngrid=((1-0)/0.01)1;

y=zeros(ngrid,1);

What will be a compact way to define y?

Steven Lord
on 18 Jun 2020

As Walter Roberson said in a comment if you want to preallocate y to be the same size as x, you can do that using functions like zeros, ones, nan, inf, etc.

x = 0:0.1:1;

y = zeros(size(x))

But depending on what calculations you're performing to "calculate the value of y for each grid point of x" you may be able to just do those calculations. For instance, the .^ operator is an element-wise operator that can operate on an array of data.

x = 0:0.1:1;

z = x.^2; % Raise each element of x to the second power, no preallocation required

% Show the results

plot(x, z)

I have one other general comment. I've noticed you've been asking a lot of pretty basic questions about MATLAB, so I suspect you're new to using it. Is that correct? If so you may want to work through the MATLAB Onramp tutorial. It teaches the basic foundations of MATLAB and so may give you solid footing as you learn more about MATLAB and start writing more complicated MATLAB code.

Suraj Sudheer Menon
on 14 Jun 2020

An approach could be to perform elementwise multiplication and perform transpose operation to obtain the a column vector.If a row vector of the same characteristics are needed the transpose operation can be avoided.

y=(x.*0)' ; (column vector);

y=(x.*0) ; (row vector)

Walter Roberson
on 18 Jun 2020

Opportunities for recent engineering grads.

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

Start Hunting!
## 7 Comments

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880748

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880748

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880751

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880751

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880766

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880766

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880778

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880778

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880790

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_880790

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_897174

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_897174

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_897636

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/540719-how-can-one-define-this-vector-in-an-elegant-way#comment_897636

Sign in to comment.