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?

7 件のコメント

madhan ravi
madhan ravi 2020 年 6 月 2 日
Could you be more explanatory?
David Hill
David Hill 2020 年 6 月 2 日
I don't understand what you are asking. ngrid does not make sense. What do you want y to be (provide and example).
alpedhuez
alpedhuez 2020 年 6 月 2 日
Let me say, x is the grid between 0 and 1.
I want to calculate the value of y for each grid point of x. That is, I want to calculate the value of someting for each x=0,0.01,0.02,... For that purpose, I want to initialize a vector y that can store a value of somthing for each value of x.
Walter Roberson
Walter Roberson 2020 年 6 月 2 日
y=zeros(size(x)) ;
alpedhuez
alpedhuez 2020 年 6 月 2 日
Thank you.
darova
darova 2020 年 6 月 14 日
y = x*0;
Suraj Sudheer Menon
Suraj Sudheer Menon 2020 年 6 月 14 日
An approach could be to perform elementwise multiplication and perform transpose operation to obtain the a column vector.
y=(x.*0)' ;

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

 採用された回答

Steven Lord
Steven Lord 2020 年 6 月 18 日

2 投票

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.

その他の回答 (1 件)

Suraj Sudheer Menon
Suraj Sudheer Menon 2020 年 6 月 14 日

0 投票

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)

3 件のコメント

alpedhuez
alpedhuez 2020 年 6 月 18 日
What does y=(x.*0)' mean?
madhan ravi
madhan ravi 2020 年 6 月 18 日
*
Walter Roberson
Walter Roberson 2020 年 6 月 18 日
.* is element-by-element multiplication. x.*0 is an all-zero array the same size as x. (x.*0)' is the conjugate transpose of that all-zero array, and since 0 is real, that is the same as the plain transpose of the all-zero array. When x is a row vector, the all-zero array would be a row vector and the transpose of it would be a column vector of zeros with the same number of elements as x had.

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

カテゴリ

製品

リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by