Save the values of a function in a for loop
2 ビュー (過去 30 日間)
古いコメントを表示
Austin Hernandez
2020 年 4 月 27 日
コメント済み: Mrutyunjaya Hiremath
2020 年 4 月 27 日
I need to record the y-values of a line from x=a to x=b.
Each loop, the slope of the line will change so there will be a different set of x and y's for each loop
How can I record the y-values from the function for each loop? The above fix doesn't work if a function is inside of it. This is my code and gives me an error once it trys to record y(i):
function for_test
x = 0:1:10;
y = ones(size(x)) ;
for i=1:10
y(i) = x+rand;
y % use y(i) so that it is written as a vector
end
end
2 件のコメント
Mohammad Sami
2020 年 4 月 27 日
The problem is this line
y(i) = x+rand;
The variable x is length 11
x+rand; % this would generate an output of length 11
However you are trying to assign it to a single value of y
y(i) % this is lenght 1
Therefore you are getting an error in assignment.
採用された回答
Mrutyunjaya Hiremath
2020 年 4 月 27 日
Hell Austin Hernandez,
you are correct. but Option01 is good.
Option 01:
function for_test
x = 0:1:10;
y = zeros(size(x)) ;
for i=1:10
y(i,:) = x+rand;
y % use y(i) so that it is written as a vector
end
end
Option 02:
function for_test
x = 0:1:10;
y = {}; %ones(size(x)) ;
for i=1:10
y{i} = x+rand;
y % use y(i) so that it is written as a vector
end
end
5 件のコメント
Mrutyunjaya Hiremath
2020 年 4 月 27 日
@ Austin,
Thank you
And, Initialize the values with One NOT Zero ...
c = 1;
m = 1;
u = 1;
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!