I would like to write a for loop to store all values of y when A=1,2,3,4,5. into a variable y1,y2,y3,y4,y5 respectively. Any help will be greatly appreciated. Thanks
3 views (last 30 days)
Show older comments
Rufus Adjetey
on 23 May 2022
Commented: Rufus Adjetey
on 23 May 2022
x = -3:0.1:3;
for A = 1:1:5
y = A*sin(x);
end
plot(x,y)
0 Comments
Accepted Answer
James Tursa
on 23 May 2022
Edited: James Tursa
on 23 May 2022
No loop needed, and no need to create multiple variables to hold results. Just use implicit array expansion and hold results in a 2D matrix. E.g.,
x = -3:0.1:3; % row vector
A = (1:1:5)'; % column vector
y = A.*sin(x); % implicit array expansion used here, matrix = column .* row
plot(x,y)
5 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!