How can I create an array with a for loop?
427 次查看(过去 30 天)
显示 更早的评论
Hi I’m trying to create an array of values that are output from a function. The function goes from -20 to 50 but I can’t create an array that large so I’m confused on what to do. The attached picture is what I have so far and it’s not working.
% code
clear all;
close all;
A = zeros(1:70);
for m = 1:70;
n = -50:20;
X = power(0.8,n)
A(m) = X
end
stem(n,arrayofA(n))
1 个评论
Stephen23
2018-3-12
Do not use the accepted answer: a loop is NOT required:
n = -50:20;
A = power(0.8,n);
回答(2 个)
Torsten
2018-3-12
A = zeros(1,71);
n = -50:20;
A = power(0.8,n)
1 个评论
Jos (10584)
2018-3-12
This is preferred over the solution with a for-loop. However, there is no need to initialise A with zeros first, since you overwrite it at the third line.
Birdman
2018-3-12
编辑:Birdman
2018-3-12
Try this:
A=zeros(1,70);n=-50:20;
for m=1:numel(n)
A(m)=power(0.8,n(m));
end
stem(n,A)
5 个评论
Dennis M
2021-8-24
y = zeros(1,12);
for i = 1:12
y(i+1) = y(i) + ((5 - (4/50)*y(i)));
end
y
x = zeros(1,10);
for i = 10:-1:2
x(i-1) = x(i) + ((5 + (4/50)*x(i)));
end
x
Good Day,
May I ask how can I insert the start and stop variable on above code? for example I want to start in 17 to 30 or 15 to 2 with decrementing step.
Thanks and Regards,
Dennis
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!