I have tried to make a loop where it displays all the answers in a matrix but it doesn't give me the 21x3 matrix. Am i using wrong commands or am i missing something.
1 次查看(过去 30 天)
显示 更早的评论
t=0;
for v=[t,sin(t),cos(t)]
if t<21
disp(v)
t=t+1;
end
end
采纳的回答
Luna
2018-11-14
Hi Merlin,
try this code below:
v = [];
for t = 0:1:20
v(t+1,:) = [t,sin(t),cos(t)] ;
end
2 个评论
Luna
2018-11-15
actually I prefer to define v = nan(21,3). Pre-allocation of the size increases the performance, and speed up the for loop.
defining empty ( v = [] ) is something about usage.
For example: if you use your for loop in if-else block, defining empty in the beginning helps you understand whether it goes to if or else block, etc.
You can easily check if isempty(v) that your matrix filled with values or remained empty at end of the process.
更多回答(0 个)
另请参阅
类别
在 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!